user.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. var config = require('./config.js');
  2. var http = require('./http.js');
  3. var common = require('./common.js');
  4. var storage = require('./storage.js');
  5. function login(callBack) {
  6. if (!callBack) {
  7. callBack = function() {};
  8. }
  9. var userInfo = storage.getUserInfo();
  10. if (!userInfo) {
  11. uni.login({
  12. success: (res) => {
  13. const accountInfo = uni
  14. .getAccountInfoSync(); // 发送 res.code 到后台换取 openId, sessionKey, unionId
  15. var postData = {
  16. code: res.code,
  17. appid: accountInfo.miniProgram.appId
  18. };
  19. http.getRequest(config.API_USER_LOGIN, postData, function(res) {
  20. if (res.data.code == 200) {
  21. storage.setUserInfo(res.data.data.baseInfo);
  22. storage.setUserToken(res.data.data.baseInfo.token);
  23. callBack(res.data.data.baseInfo);
  24. } else {
  25. common.alert('登录提示', res.data.msg);
  26. callBack(null);
  27. }
  28. });
  29. }
  30. });
  31. }
  32. callBack(userInfo);
  33. } // 登录从本地找openid 没有的话从本地加载
  34. function getUserOpenId() {
  35. if (!uni.getStorageSync(config.STORAGE_OPEN_ID)) {
  36. uni.login({
  37. success: (res) => {
  38. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  39. var postData = {
  40. wx_code: res.code
  41. };
  42. http.postApi(config.API_GET_OPEN_ID, postData, function(res) {
  43. if (res.data.code == 200) {
  44. uni.setStorageSync(config.STORAGE_OPEN_ID, res.data.data.wx_session_msg
  45. .openid);
  46. if (res.data.data.token) {
  47. storage.setUserToken(res.data.data.token); // wx.setStorage({
  48. // key: config.STORAGE_USER_TOKEN,
  49. // data: res.data.data.token,
  50. // })
  51. }
  52. }
  53. });
  54. }
  55. });
  56. }
  57. }
  58. function getUserInfo(callBack) {
  59. var userInfo = storage.getUserInfo();
  60. if (userInfo) {
  61. if (userInfo.is_new == 0) {
  62. if (callBack) callBack(userInfo);
  63. return;
  64. }
  65. uni.getUserInfo({
  66. success: function(res) {
  67. // 上报
  68. http.postApi(config.API_REPORT_USER_INFO, res.userInfo, function(rs2) {
  69. if (rs2.data.code == 200) {
  70. storage.setUserInfo(rs2.data.data.baseInfo);
  71. callBack(rs2.data.data.baseInfo);
  72. } else {
  73. common.alert('登录提示', rs2.data.msg);
  74. }
  75. });
  76. },
  77. fail: function(res) {
  78. common.alert('请授权', '商城需要获取您的用户信息', function() {
  79. uni.openSetting({
  80. success: function(rs) {}
  81. });
  82. });
  83. },
  84. complete: function(res) {}
  85. });
  86. }
  87. login(callBack);
  88. } // 支付
  89. function wxPay(order_sn, payParams, callBack) {
  90. common.loading();
  91. const pData =
  92. uni.requestPayment({
  93. timeStamp: payParams.timeStamp,
  94. nonceStr: payParams.nonceStr,
  95. package: payParams.package,
  96. signType: payParams.signType,
  97. paySign: payParams.paySign,
  98. success: function(rs) {
  99. uni.showToast({
  100. title: '支付成功',
  101. icon: 'success',
  102. duration: 1500
  103. });
  104. callBack(
  105. true
  106. ) // http.postApi(config.API_USER_PAY_SUCCESS, { order_sn: order_sn }, function (rs2) {
  107. // wx.hideLoading();
  108. // if (rs2.data.code == 200) {
  109. // // 跳转到订单页面
  110. // callBack(true);
  111. // } else {
  112. // common.simpleToast(rs2.data.msg);
  113. // }
  114. // })
  115. },
  116. fail: function(res) {
  117. // 跳转到订单页面
  118. common.simpleToast('未完成支付');
  119. callBack(false);
  120. },
  121. complete: function(res) {
  122. uni.hideLoading();
  123. }
  124. });
  125. } // 取消租赁支付
  126. function cancelHirePay(order_sn) {
  127. http.postApi(
  128. config.API_DAY_HIRE_CABINRT_CANCEL_CABINRT_CHANGE_BATTERY, {
  129. order_sn: order_sn
  130. },
  131. function(res) {
  132. if (res.data.code == 200) {}
  133. }
  134. );
  135. } // 取消门店换电支付
  136. function cancelShopExPay(order_sn) {
  137. http.postApi(
  138. config.API_CANCEL_SHOP_EX, {
  139. exchange_sn: order_sn
  140. },
  141. function(res) {
  142. if (res.data.code == 200) {}
  143. }
  144. );
  145. } // 取消电柜换电支付
  146. function cancelCabExPay(order_sn) {
  147. http.postApi(
  148. config.API_CANCEL_EX_PAY, {
  149. order_sn: order_sn
  150. },
  151. function(res) {
  152. if (res.data.code == 200) {}
  153. }
  154. );
  155. }
  156. function cancelExtractPay(stash_sn, target_id) {
  157. http.postApi(
  158. config.API_CANCEL_EXTRACT_PAY, {
  159. stash_sn: stash_sn,
  160. target_id: target_id
  161. },
  162. function(res) {
  163. if (res.data.code == 200) {}
  164. }
  165. );
  166. } //取消充电柜换电支付
  167. function cancelChargeExPay(order_sn) {
  168. http.postApi(
  169. config.API_CANCEL_CHARGE_EX_PAY, {
  170. order_sn: order_sn
  171. },
  172. function(res) {
  173. if (res.data.code == 200) {}
  174. }
  175. );
  176. } // 取消充电柜支付
  177. function cancelChargePay(order_sn) {
  178. http.postApi(
  179. config.API_CANCEL_CHARGE_PAY, {
  180. order_sn: order_sn
  181. },
  182. function(res) {
  183. if (res.data.code == 200) {}
  184. }
  185. );
  186. } //取消充电柜取电支付
  187. function cancelChargeExtractPay(order_sn) {
  188. http.postApi(
  189. config.API_CHARGE_EXTRACT_CANCEL, {
  190. order_sn: order_sn
  191. },
  192. function(res) {
  193. if (res.data.code == 200) {}
  194. }
  195. );
  196. } // 取消订单
  197. function cancelOrder(order_sn, callBack) {
  198. http.postApi(
  199. config.API_USER_CANCEL_ORDER, {
  200. order_sn: order_sn
  201. },
  202. function(res) {
  203. if (res.data.code == 200) {
  204. callBack();
  205. } else {
  206. common.simpleToast(res.data.msg);
  207. }
  208. }
  209. );
  210. }
  211. function loadCarNum() {
  212. http.postApi(config.API_USER_CART_NUM, {}, function(response) {
  213. if (response.data.code === 200) {
  214. if (response.data.data.cart_num) {
  215. uni.setTabBarBadge({
  216. index: 2,
  217. text: response.data.data.cart_num
  218. });
  219. } else {
  220. uni.removeTabBarBadge({
  221. index: 2
  222. });
  223. }
  224. }
  225. });
  226. }
  227. function aliPay(trade_no, callBack) {
  228. my.tradePay({
  229. tradeNO: trade_no,
  230. success: function(res) {
  231. if (res.resultCode == 9000) {
  232. uni.showToast({
  233. title: '支付成功',
  234. icon: 'success',
  235. duration: 1500
  236. });
  237. callBack(true) // 支付成功后执行的回调函数,传入true表示支付成功,false表示支付失败
  238. } else {
  239. my.alert({
  240. content: '支付失败',
  241. });
  242. callBack(false)
  243. }
  244. },
  245. fail: function(res) {
  246. callBack(false)
  247. },
  248. })
  249. }
  250. function totalPay(order_sn, payParams, trade_no, callBack) {
  251. //#ifdef MP-WEIXIN
  252. wxPay(order_sn, payParams, function(isSuccess) {
  253. if (isSuccess) {
  254. callBack(true)
  255. } else {
  256. callBack(false)
  257. }
  258. })
  259. //#endif
  260. //#ifdef MP-ALIPAY
  261. aliPay(trade_no, function(isaliPaySuccess) {
  262. if (isaliPaySuccess) {
  263. callBack(true)
  264. } else {
  265. callBack(false)
  266. }
  267. })
  268. //#endif
  269. }
  270. // 取消租车/购车支付
  271. function cancelPayTakeCar(order_sn) {
  272. http.postApi(
  273. config.API_FLK_ORDER_CANCEL_HIRE, {
  274. order_sn: order_sn
  275. },
  276. function(res) {
  277. if (res.data.code == 200) {}
  278. }
  279. );
  280. }
  281. module.exports = {
  282. login: login,
  283. getUserInfo: getUserInfo,
  284. wxPay: wxPay,
  285. cancelOrder: cancelOrder,
  286. loadCarNum: loadCarNum,
  287. cancelHirePay: cancelHirePay,
  288. cancelShopExPay: cancelShopExPay,
  289. cancelCabExPay: cancelCabExPay,
  290. cancelChargePay: cancelChargePay,
  291. cancelChargeExtractPay: cancelChargeExtractPay,
  292. cancelExtractPay: cancelExtractPay,
  293. cancelChargeExPay: cancelChargeExPay,
  294. totalPay: totalPay,
  295. getUserOpenId: getUserOpenId,
  296. cancelPayTakeCar: cancelPayTakeCar,
  297. };