request.js 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. const app = getApp();
  2. const env = {
  3. NODE_ENV: 'dev',
  4. dev: {
  5. apiUrl: 'https://pkapi.zhiqiyun.com'
  6. },
  7. prd: {
  8. apiUrl: 'https://pkapi.pikadongli.com'
  9. },
  10. pota: {
  11. apiUrl: 'http://192.168.110.182:8098' ,//zf
  12. // apiUrl: 'http://192.168.110.180:8098' ,//wjg
  13. }
  14. }
  15. var header = {
  16. 'content-type': 'application/x-www-form-urlencoded',
  17. 'appId': 'ZQ4OItRePFqbge5', //享团
  18. }
  19. /**小程序配置 */
  20. const publics = {
  21. picUrls: 'https://zhiqiyun.oss-cn-hangzhou.aliyuncs.com/static/', //图片地址
  22. 'copyrighttitle': '智企云', //底部版权公司名称
  23. 'copyrighttel': '400-698-5980', //底部版权服务电话
  24. 'copyrightlogo': 'zhiqiyun', //底部版权 logo 文件名
  25. 'yaomeng': 'https://cfwz.yaomengwang.cn/hunan?enterpriseCode=hnty&orderId=', //药盟接口地址
  26. 'storemanagers': 'https://websmall.zhiqiyun.com?appId=', //saas门店管理地址(智企云所有)
  27. 'channel': '', //处方中间渠道号 SAAS系统为空
  28. 'is_customer_pick_up': false,
  29. 'mapLBSKEY': 'I52BZ-OXYKJ-PWMFN-XWADQ-WR775-XTBQE',
  30. 'iconSrc': 'red/',
  31. 'titleTopBgColor': '#EB692F',
  32. }
  33. /*
  34. * 获取token
  35. */
  36. const getToken = (success) => {
  37. let token = getStorage('AUTH_TOKEN');
  38. if (token) {
  39. success.call(this, token);
  40. return false;
  41. } else {
  42. loadToken(data => {
  43. success.call(this, data);
  44. });
  45. }
  46. }
  47. const loadToken = (success) => {
  48. let that = this
  49. // uni.request({
  50. // url: env[env.NODE_ENV].apiUrl + '/api/access_token',
  51. // data: {},
  52. // method: 'get',
  53. // header: header,
  54. // success(json) {
  55. // if (json.statusCode !== 200) return msg(json.data.message);
  56. // if (json.data.code !== 0) return msg(json.data.msg)
  57. // setStorage('AUTH_TOKEN', json.data.data);
  58. // success.call(that, json.data.data);
  59. // },
  60. // fail() {
  61. // msg('access_token 获取失败');
  62. // }
  63. // })
  64. console.log('token')
  65. let token = getStorage('AUTH_TOKEN');
  66. if (token) {
  67. success.call(this, token);
  68. return false;
  69. } else {
  70. // msg('access_token 获取失败');
  71. removeStorage('userInfo')
  72. // return redirectIndex();
  73. }
  74. }
  75. /**
  76. * 微信小程序静默授权登录
  77. * @param {*} parentId
  78. * @param {*} merchantId
  79. */
  80. const silenceLogin = (parentId, merchantId) => {
  81. // #ifdef MP-WEIXIN
  82. if (!isLogins(false)) { //未登录
  83. uni.login({
  84. success(res) {
  85. console.log('静默授权登录>>>', res);
  86. let params = {
  87. code: res.code,
  88. parentId: parentId ? parentId : '',
  89. merchantId: merchantId ? merchantId : ''
  90. }
  91. return new Promise((resolve, reject) => {
  92. postRequest('/api/v3/silence/login', params, data => {
  93. if(data.token) setStorage('AUTH_TOKEN',data.token);
  94. getRequest('/api/user/my', {}, suc => {
  95. data.isDistriUser = suc.isDistriUser;
  96. setStorage('userInfo', data);
  97. });
  98. resolve();
  99. });
  100. })
  101. }
  102. });
  103. }
  104. // #endif
  105. }
  106. const getWXUserProfile = (suc) => {
  107. let that = this;
  108. var sessionKey = '';
  109. var openid = '';
  110. getApp().globalData.getCheckSessoin(json => {
  111. sessionKey = json.session_key;
  112. openid = json.openid;
  113. });
  114. try {
  115. uni.getUserProfile({
  116. desc: '完善信息',
  117. success: res => {
  118. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  119. },
  120. fail: res => {
  121. console.log('授权失败数据>>>', res);
  122. msg(res);
  123. }
  124. });
  125. } catch (e) {
  126. // 老版
  127. wx.getUserInfo({
  128. success: function(ress) {
  129. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  130. }
  131. });
  132. }
  133. }
  134. /**
  135. * 保存微信头像
  136. */
  137. const updataWxInfos = (iv, encryptedData, sessionKey, openid, suc) => {
  138. if (!encryptedData || !iv) return false;
  139. let _params = {
  140. sessionKey: sessionKey,
  141. openId: openid,
  142. iv: iv,
  143. encryptedData: encryptedData
  144. };
  145. postRequest('/api/user/weixin/update', _params, json => {
  146. setStorage('userInfo', json);
  147. suc.call();
  148. });
  149. }
  150. const clearValueEmpty = (data) => {
  151. let keyValue = {};
  152. for (let key in data) {
  153. let value = typeof data[key];
  154. if (value == 'string' && value) {
  155. if (data[key] != 'undefined' && data[key] != " " && data[key] != undefined && data[key] != null) {
  156. keyValue[key] = data[key];
  157. }
  158. // keyValue[key] = data[key];
  159. } else if (value == 'number' && value != null) {
  160. keyValue[key] = data[key];
  161. } else if (value == 'boolean') {
  162. keyValue[key] = data[key];
  163. } else {
  164. if (data[key]) keyValue[key] = data[key];
  165. }
  166. }
  167. return keyValue;
  168. }
  169. /**
  170. * 公共请求
  171. *
  172. * @param {*} url 请求url
  173. * @param {*} data 请求参数
  174. * @param {*} method 请求方法
  175. * @param {*} success 成功函数
  176. * @param {*} isLoad 是否显示加载提示
  177. */
  178. const baseRequest = (url, data, method, success, isLoad) => {
  179. if(url=='/admin/v2/login'||url=='/api/protocol'||url=='/api/params/value'||url=='/api/other/config'||url=='/admin/v2/user/feedback'){
  180. if (!isLoad) uni.showNavigationBarLoading();
  181. if (url.indexOf('/v1') != -1) {
  182. header['content-type'] = 'application/x-www-form-urlencoded'
  183. } else {
  184. header['content-type'] = 'application/json;charset=UTF-8'
  185. }
  186. uni.request({
  187. url: env[env.NODE_ENV].apiUrl + url,
  188. data: clearValueEmpty(data),
  189. method: method,
  190. header: header,
  191. success(json) {
  192. if (!isLoad) uni.hideNavigationBarLoading();
  193. if (json.statusCode !== 200) return msg(json.data.message);
  194. if (json.data.code === 10021 || json.data.code === 10020) {
  195. // console.log("第二次执行")
  196. removeStorage('AUTH_TOKEN');
  197. return redirectIndex();
  198. }
  199. if (json.data.code !== 0) {
  200. return msg(json.data.msg)
  201. }
  202. // console.log("第二次执行")
  203. let data = json.data.data;
  204. if (typeof data === 'string' && data.indexOf("{") === 0) {
  205. data = JSON.parse(data);
  206. }
  207. success.call(this, data);
  208. },
  209. fail() {
  210. if (!isLoad) uni.hideNavigationBarLoading();
  211. },
  212. })
  213. }
  214. else{
  215. getToken(token => {
  216. if (!isLoad) uni.showNavigationBarLoading();
  217. header.authorityToken = token;
  218. if (url.indexOf('/v1') != -1) {
  219. header['content-type'] = 'application/x-www-form-urlencoded'
  220. } else {
  221. header['content-type'] = 'application/json;charset=UTF-8'
  222. }
  223. uni.request({
  224. url: env[env.NODE_ENV].apiUrl + url,
  225. data: clearValueEmpty(data),
  226. method: method,
  227. header: header,
  228. success(json) {
  229. if (!isLoad) uni.hideNavigationBarLoading();
  230. if (json.statusCode !== 200) return msg(json.data.message);
  231. if (json.data.code === 10021 || json.data.code === 10020) {
  232. // console.log("第二次执行")
  233. removeStorage('AUTH_TOKEN');
  234. removeStorage('userInfo');
  235. return redirectIndex();
  236. }
  237. if (json.data.code !== 0) {
  238. return msg(json.data.msg)
  239. }
  240. // console.log("第二次执行")
  241. let data = json.data.data;
  242. if (typeof data === 'string' && data.indexOf("{") === 0) {
  243. data = JSON.parse(data);
  244. }
  245. success.call(this, data);
  246. },
  247. fail() {
  248. if (!isLoad) uni.hideNavigationBarLoading();
  249. },
  250. // complete(){
  251. // success.call(this, data);
  252. // },
  253. })
  254. })
  255. }
  256. }
  257. const loadIng = (msg) => {
  258. uni.showLoading({
  259. title: msg,
  260. mask: true
  261. })
  262. }
  263. /**POST请求 */
  264. const postRequest = (url, data, success, isLoad) => {
  265. header['content-type'] = 'application/json;charset=UTF-8'
  266. baseRequest(url, data, 'post', success, isLoad);
  267. }
  268. const postRequestLoding = (url, data, success, isLoding) => {
  269. header['content-type'] = 'application/json;charset=UTF-8'
  270. baseRequest(url, data, 'post', success, false, isLoding);
  271. }
  272. /**GET请求 */
  273. const getRequest = (url, data, success, isLoad) => {
  274. header['content-type'] = 'application/json;charset=UTF-8'
  275. baseRequest(url, data, 'get', success, isLoad);
  276. }
  277. /**Put请求 */
  278. const putRequest = (url, data, success, isLoad) => {
  279. header['content-type'] = 'application/json;charset=UTF-8'
  280. baseRequest(url, data, 'put', success, isLoad)
  281. }
  282. const putRequestJson = (url, data, success, isLoad) => {
  283. header['content-type'] = 'application/json;charset=UTF-8'
  284. baseRequest(url, data, 'put', success, isLoad)
  285. }
  286. /**
  287. * 上传文件
  288. * @param {*} url 请求url
  289. * @param {*} data 携带数据
  290. * @param {*} success 请求成功函数
  291. */
  292. const uploadFile = (url, data, success) => {
  293. // console.log(env[env.NODE_ENV].apiUrl + url, data)
  294. getToken(token => {
  295. uni.uploadFile({
  296. url: env[env.NODE_ENV].apiUrl + url,
  297. filePath: data,
  298. name: 'file',
  299. header: {
  300. 'appId': header['appId'],
  301. 'authorityToken': token
  302. },
  303. success(res) {
  304. if (res.statusCode !== 200) return msg('文件上传失败');
  305. let data = JSON.parse(res.data);
  306. if (data.code !== 0) return msg(data.msg);
  307. success.call(this, data.data);
  308. },
  309. })
  310. })
  311. }
  312. /**弹窗 */
  313. const msg = (title, success) => {
  314. if (title) {
  315. uni.showToast({
  316. title: title,
  317. icon: 'none',
  318. success() {
  319. if (success) success.call(this);
  320. }
  321. });
  322. }
  323. return false;
  324. }
  325. const msgConfirm = (msg, success, cancel) => {
  326. uni.showModal({
  327. title: '提示',
  328. content: msg,
  329. success(res) {
  330. if (res.confirm) {
  331. success.call(this);
  332. } else {
  333. if (cancel) cancel.call(this);
  334. }
  335. }
  336. })
  337. }
  338. const msgConfirmText = (msg, confirmText, success, cancel) => {
  339. uni.showModal({
  340. title: '提示',
  341. content: msg,
  342. confirmText: confirmText,
  343. success(res) {
  344. if (res.confirm) {
  345. success.call(this);
  346. } else {
  347. if (cancel) cancel.call(this);
  348. }
  349. }
  350. })
  351. }
  352. const alertMsg = (msg, success) => {
  353. uni.showModal({
  354. title: '提示',
  355. content: msg,
  356. showCancel: false,
  357. success(res) {
  358. if (success) success.call(this);
  359. }
  360. })
  361. return false;
  362. }
  363. const load = msg => {
  364. uni.showLoading({
  365. title: msg,
  366. mask: true
  367. })
  368. }
  369. const g = (url, success, isLoad) => {
  370. getRequest(url, {}, success, isLoad)
  371. }
  372. const p = (url, success, isLoad) => {
  373. postRequest(url, {}, success, isLoad)
  374. }
  375. const send = (url, mobile, success, error) => {
  376. if (!mobile) {
  377. return msg('手机号码不能为空', error);
  378. }
  379. postRequest(url, {
  380. phone: mobile
  381. }, json => {
  382. if (json.code !== 0) {
  383. return msg(json.msg, error);
  384. }
  385. let time = 60;
  386. const initTime = setInterval(() => {
  387. time--;
  388. if (time > 0) {
  389. success.call(this, time + '秒后获取');
  390. } else {
  391. clearInterval(initTime);
  392. success.call(this, '获取验证码');
  393. }
  394. }, 1000)
  395. })
  396. }
  397. // 登陆拦截
  398. const redirectIndex = (params) => {
  399. // uni.clearStorageSync();
  400. // console.log(params)
  401. var pages = getCurrentPages();
  402. // console.log("pages》》》》》登陆拦截", pages);
  403. let currentUrl;
  404. if (pages) {
  405. var currentPage = pages[pages.length - 1];
  406. if (currentPage) {
  407. currentUrl = currentPage.route;
  408. let query = currentPage.data.query;
  409. if (query) {
  410. for (let key in query) {
  411. const fo = key + '=' + query[key];
  412. currentUrl += currentUrl.indexOf('?') > -1 ? '&' + fo : '?' + fo;
  413. }
  414. }
  415. if (currentUrl) {
  416. console.log('currentUrl》》》》》', currentUrl);
  417. setStorage("REDIRECT_URL", '/' + currentUrl)
  418. if (currentUrl == 'pages/authorize/authorize') {
  419. console.log('当前已经处于登录页面,不在向下执行跳转');
  420. return
  421. }
  422. }
  423. }
  424. }
  425. let redirectUrl = '/pages/authorize/authorize' + (params ? params : '');
  426. // console.log("登陆拦截")
  427. // uni.navigateTo({
  428. // url: redirectUrl
  429. // })
  430. var jump = getStorage('authorize_jump') //以下解决多次跳转登录页的重点
  431. if (!jump) {
  432. //以下做token失效的操作
  433. setTimeout(() => {
  434. uni.navigateTo({
  435. url: redirectUrl
  436. })
  437. }, 100)
  438. setStorage('authorize_jump', 'true')
  439. }
  440. return false;
  441. }
  442. const saveImage = (url) => {
  443. uni.saveImageToPhotosAlbum({
  444. filePath: url,
  445. success() {
  446. msg('图片保存成功');
  447. }
  448. })
  449. }
  450. const saveImageToPhotosAlbum = (url) => {
  451. if (!url) return msg('小程序码不存在');
  452. uni.getImageInfo({
  453. src: url,
  454. success(json) {
  455. // uni.getSetting({
  456. // success(res) {
  457. // if (!res.authSetting['scope.writePhotosAlbum']) {
  458. // uni.authorize({
  459. // scope: 'scope.writePhotosAlbum',
  460. // success() {
  461. // saveImage(json.path);
  462. // }
  463. // })
  464. // } else {
  465. // saveImage(json.path);
  466. // }
  467. // }
  468. // })
  469. }
  470. })
  471. }
  472. const getConfig = (config, id) => {
  473. let con = {};
  474. config.info.forEach(inf => {
  475. // console.log(inf.version, inf.version ? inf.version.indexOf(id) : '-')
  476. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  477. con[inf.setName] = inf.setDefault;
  478. });
  479. config.card.forEach(inf => {
  480. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  481. con[inf.setName] = inf.setDefault;
  482. });
  483. config.realTime.forEach(inf => {
  484. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  485. con[inf.setName] = inf.setDefault;
  486. });
  487. return con;
  488. }
  489. const isLogin = async (error) => {
  490. let userInfo = getStorage('userInfo');
  491. if (!userInfo || !userInfo.id) {
  492. if (error) return false;
  493. // console.log("第一次执行")
  494. return redirectIndex();
  495. }
  496. return true;
  497. }
  498. //检测是否登录
  499. const isLogins = (isGoLogin) => {
  500. let userInfo = getStorage('userInfo');
  501. if (!userInfo) {
  502. if (isGoLogin) {
  503. redirectIndex();
  504. }
  505. return false;
  506. }
  507. return true;
  508. }
  509. const setStorage = (key, value) => {
  510. uni.setStorageSync(env.NODE_ENV + "_" + key, value)
  511. }
  512. const getStorage = (key) => {
  513. return uni.getStorageSync(env.NODE_ENV + "_" + key)
  514. }
  515. const removeStorage = (key) => {
  516. return uni.removeStorageSync(env.NODE_ENV + "_" + key);
  517. }
  518. /**
  519. * 缓存模板id
  520. */
  521. // const gettmplIds = () => {
  522. // getRequest('/api/wxmes/querylist', {}, res => {
  523. // setStorage('tmplIds', res)
  524. // console.log(res)
  525. // })
  526. // }
  527. //微信支付订单公共方法
  528. const payOrder = (id,success) => {
  529. var loctionAddressMap = getStorage('loctionAddressMap');
  530. var datas = {
  531. id: id
  532. };
  533. if (loctionAddressMap) {
  534. datas.province = loctionAddressMap.province;
  535. datas.city = loctionAddressMap.city;
  536. };
  537. if(getStorage('isVideoScene')){
  538. uni.checkBeforeAddOrder({
  539. success (res){
  540. console.log('下单前置检查',res)
  541. let data = res.data;
  542. data.requiredFundType = 0;
  543. datas.sceneCheckStr = JSON.stringify(data);
  544. wexinPay(datas,success)
  545. },
  546. })
  547. }else{
  548. wexinPay(datas,success)
  549. }
  550. }
  551. //支付中或已支付
  552. const payStatus = (json, id, success) =>{
  553. if(json&&json.code === 115){
  554. setTimeout(res=>{
  555. uni.hideLoading();
  556. isShowLoading = false;
  557. uni.showModal({
  558. title: '提示',
  559. content: '当前订单正在支付或30秒后重试',
  560. confirmText: '我知道了',
  561. showCancel: false,
  562. success(res) {
  563. }
  564. })
  565. },3000)
  566. return false;
  567. }
  568. if (json.type === 2 || json.type === 3) {
  569. //调用后台判断订单是否支付成功
  570. let i = 0;
  571. let timer = setInterval(() => {
  572. postRequest('/api/order/check', {
  573. id: id
  574. }, res => {
  575. if (res.isSuccess || i === 4) {
  576. clearInterval(timer);
  577. success.call(this, res.isSuccess);
  578. } else {
  579. i++;
  580. }
  581. }, true)
  582. }, 200);
  583. // success.call(this);
  584. return false;
  585. }
  586. }
  587. // 支付成功回调
  588. const paySuccess = (id,success) =>{
  589. console.log('支付成功')
  590. let i = 0;
  591. let timer = setInterval(() => {
  592. postRequest('/api/order/check', {
  593. id: id
  594. }, res => {
  595. console.log(i)
  596. if (res.isSuccess || i === 5) {
  597. clearInterval(timer);
  598. success.call(this, res.isSuccess);
  599. } else {
  600. i++;
  601. }
  602. }, true)
  603. }, 500);
  604. }
  605. // 支付失败回调
  606. const payFail = (id) =>{
  607. console.log('支付失败')
  608. postRequest('/api/order/cancelPay', {
  609. id: id
  610. }, data => {
  611. uni.redirectTo({
  612. url: '/mine/orderDet/orderDet?id=' + id,
  613. })
  614. })
  615. }
  616. const wexinPay = (datas, success) => {
  617. let isShowLoading = false;
  618. if (!isShowLoading) {
  619. loadIng('加载中');
  620. // console.log('加载中')
  621. isShowLoading = true;
  622. }
  623. // #ifdef MP-WEIXIN
  624. postRequest('/api/order/weixin/pay', datas, json => {
  625. // console.log('json',json)
  626. payStatus(json, datas.id, success)
  627. const sceneCheckStr = datas.sceneCheckStr;
  628. if(sceneCheckStr && JSON.parse(sceneCheckStr).requiredFundType === 1){
  629. console.log('拉起收银台')
  630. wx.requestOrderPayment({
  631. timeStamp: json.timeStamp,
  632. nonceStr: json.nonceStr,
  633. package: json.packages,
  634. signType: json.signType,
  635. orderInfo: json.orderInfo,
  636. paySign: json.sign,
  637. success: function() {
  638. paySuccess(datas.id,success)
  639. },
  640. fail: function(res) {
  641. console.log(res);
  642. payFail(datas.id)
  643. }
  644. })
  645. }else{
  646. uni.requestPayment({
  647. timeStamp: json.timeStamp,
  648. nonceStr: json.nonceStr,
  649. package: json.packages,
  650. signType: json.signType,
  651. paySign: json.sign,
  652. success: function() {
  653. paySuccess(datas.id,success)
  654. },
  655. fail: function(res) {
  656. console.log(res);
  657. payFail(datas.id)
  658. }
  659. })
  660. }
  661. if (isShowLoading) {
  662. uni.hideLoading();
  663. isShowLoading = false;
  664. }
  665. })
  666. // #endif
  667. // #ifdef MP-ALIPAY
  668. datas.payChannel = 'alipay_lite ';
  669. postRequest('/api/order/weixin/pay', datas, json => {
  670. // console.log(json)
  671. if (json.type === 2 || json.type === 3) {
  672. //调用后台判断订单是否支付成功
  673. let i = 0;
  674. let timer = setInterval(() => {
  675. postRequest('/api/order/check', {
  676. id: id
  677. }, res => {
  678. if (res.isSuccess || i === 4) {
  679. clearInterval(timer);
  680. success.call(this, res.isSuccess);
  681. } else {
  682. i++;
  683. }
  684. }, true)
  685. }, 200);
  686. // success.call(this);
  687. return false;
  688. }
  689. uni.requestPayment({
  690. provider: 'alipay',
  691. orderInfo: json.packages,
  692. success: function() {
  693. paySuccess(datas.id,success)
  694. },
  695. fail: function(res) {
  696. payFail(datas.id)
  697. console.log(res);
  698. }
  699. })
  700. if (isShowLoading) {
  701. uni.hideLoading();
  702. isShowLoading = false;
  703. }
  704. })
  705. // #endif
  706. }
  707. //微信支付订单公共方法
  708. const payOrdersss = (id, success) => {
  709. let isShowLoading = false;
  710. if (!isShowLoading) {
  711. loadIng('加载中');
  712. // console.log('加载中')
  713. isShowLoading = true;
  714. }
  715. var loctionAddressMap = getStorage('loctionAddressMap');
  716. var datas = {
  717. id: id
  718. };
  719. if (loctionAddressMap) {
  720. datas.province = loctionAddressMap.province;
  721. datas.city = loctionAddressMap.city;
  722. };
  723. // #ifdef MP-WEIXIN
  724. postRequest('/api/order/weixin/pay', datas, json => {
  725. // console.log(json)
  726. if (json.type === 2 || json.type === 3) {
  727. //调用后台判断订单是否支付成功
  728. let i = 0;
  729. let timer = setInterval(() => {
  730. postRequest('/api/order/check', {
  731. id: id
  732. }, res => {
  733. if (res.isSuccess || i === 4) {
  734. clearInterval(timer);
  735. success.call(this, res.isSuccess);
  736. } else {
  737. i++;
  738. }
  739. }, true)
  740. }, 200);
  741. // success.call(this);
  742. return false;
  743. }
  744. uni.requestPayment({
  745. timeStamp: json.timeStamp,
  746. nonceStr: json.nonceStr,
  747. package: json.packages,
  748. signType: json.signType,
  749. paySign: json.sign,
  750. success: function() {
  751. console.log('支付成功')
  752. let i = 0;
  753. let timer = setInterval(() => {
  754. postRequest('/api/order/check', {
  755. id: id
  756. }, res => {
  757. console.log(i)
  758. if (res.isSuccess || i === 5) {
  759. clearInterval(timer);
  760. success.call(this, res.isSuccess);
  761. } else {
  762. i++;
  763. }
  764. }, true)
  765. }, 500);
  766. },
  767. fail: function(res) {
  768. console.log('支付失败')
  769. postRequest('/api/order/cancelPay', {
  770. id: id
  771. }, data => {
  772. uni.redirectTo({
  773. url: '/mine/orderDet/orderDet?id=' + id,
  774. })
  775. })
  776. console.log(res);
  777. }
  778. })
  779. if (isShowLoading) {
  780. uni.hideLoading();
  781. isShowLoading = false;
  782. }
  783. })
  784. // #endif
  785. // #ifdef MP-ALIPAY
  786. datas.payChannel = 'alipay_lite ';
  787. postRequest('/api/order/weixin/pay', datas, json => {
  788. // console.log(json)
  789. if (json.type === 2 || json.type === 3) {
  790. //调用后台判断订单是否支付成功
  791. let i = 0;
  792. let timer = setInterval(() => {
  793. postRequest('/api/order/check', {
  794. id: id
  795. }, res => {
  796. if (res.isSuccess || i === 4) {
  797. clearInterval(timer);
  798. success.call(this, res.isSuccess);
  799. } else {
  800. i++;
  801. }
  802. }, true)
  803. }, 200);
  804. // success.call(this);
  805. return false;
  806. }
  807. uni.requestPayment({
  808. provider: 'alipay',
  809. orderInfo: json.packages,
  810. success: function() {
  811. console.log('支付成功')
  812. let i = 0;
  813. let timer = setInterval(() => {
  814. postRequest('/api/order/check', {
  815. id: id
  816. }, res => {
  817. console.log(i)
  818. if (res.isSuccess || i === 5) {
  819. clearInterval(timer);
  820. success.call(this, res.isSuccess);
  821. } else {
  822. i++;
  823. }
  824. }, true)
  825. }, 500);
  826. },
  827. fail: function(res) {
  828. console.log('支付失败')
  829. postRequest('/api/order/cancelPay', {
  830. id: id
  831. }, data => {
  832. uni.redirectTo({
  833. url: '/mine/orderDet/orderDet?id=' + id,
  834. })
  835. })
  836. console.log(res);
  837. }
  838. })
  839. if (isShowLoading) {
  840. uni.hideLoading();
  841. isShowLoading = false;
  842. }
  843. })
  844. // #endif
  845. }
  846. //微信支付订单公共方法 ==活动
  847. const payOrders = (id, success, type) => {
  848. let isShowLoading = false;
  849. if (!isShowLoading) {
  850. loadIng('加载中');
  851. // console.log('加载中')
  852. isShowLoading = true;
  853. }
  854. postRequest('/api/order/weixin/pay', {
  855. id: id
  856. }, json => {
  857. // console.log(json)
  858. payStatus(json, id, success)
  859. uni.requestPayment({
  860. timeStamp: json.timeStamp,
  861. nonceStr: json.nonceStr,
  862. package: json.packages,
  863. signType: json.signType,
  864. paySign: json.sign,
  865. success: function() {
  866. paySuccess(id, success)
  867. },
  868. fail: function(res) {
  869. console.log('支付失败')
  870. postRequest('/api/order/cancelPay', {
  871. id: id
  872. }, data => {
  873. uni.redirectTo({
  874. url: '/mine/activity/activity',
  875. })
  876. })
  877. console.log(res);
  878. }
  879. })
  880. if (isShowLoading) {
  881. uni.hideLoading();
  882. isShowLoading = false;
  883. }
  884. })
  885. }
  886. const authSetting = (authority, success, error) => {
  887. // console.log(authority,success)
  888. // #ifndef H5
  889. uni.getSetting({
  890. success(res) {
  891. if (res.authSetting[authority]) {
  892. success.call(this);
  893. return false;
  894. }
  895. uni.authorize({
  896. scope: authority,
  897. success() {
  898. success.call(this);
  899. },
  900. fail: function(res) {
  901. error.call(this);
  902. }
  903. })
  904. }
  905. })
  906. // #endif
  907. }
  908. const getLocation = (suss) => {
  909. // #ifdef MP-WEIXIN
  910. authSetting('scope.userLocation', () => {
  911. // load('定位中…');
  912. uni.getLocation({
  913. type: 'gcj02',
  914. // isHighAccuracy: true,
  915. success: function(res) {
  916. // uni.hideLoading();
  917. console.log(res, "789")
  918. suss.call(this, res);
  919. },
  920. fail: function(res) {
  921. console.log("调用失败", res)
  922. suss.call(this, 2)
  923. // uni.hideLoading();
  924. }
  925. })
  926. }, () => {
  927. // uni.hideLoading();
  928. console.log("取消")
  929. suss.call(this, 1)
  930. });
  931. // #endif
  932. // #ifdef MP-ALIPAY
  933. uni.getLocation({
  934. type: 'gcj02',
  935. // isHighAccuracy: true,
  936. success: function(res) {
  937. // uni.hideLoading();
  938. console.log(res, "789")
  939. suss.call(this, res);
  940. },
  941. fail: function(res) {
  942. console.log("调用失败", res)
  943. suss.call(this, 2)
  944. // uni.hideLoading();
  945. }
  946. })
  947. // #endif
  948. // #ifdef H5
  949. uni.getLocation({
  950. type: 'gcj02',
  951. isHighAccuracy: true,
  952. success: function(res) {
  953. // uni.hideLoading();
  954. console.log(res)
  955. suss.call(this, res, "789");
  956. },
  957. fail: function() {
  958. console.log("调用失败", res)
  959. suss.call(this, 2)
  960. // uni.hideLoading();
  961. }
  962. })
  963. // #endif
  964. }
  965. const scopeAddress = (success) => {
  966. // #ifdef MP-WEIXIN
  967. authSetting('scope.address', () => {
  968. uni.chooseAddress({
  969. success: function(res) {
  970. success.call(this, res);
  971. },
  972. })
  973. }, () => {
  974. msg('未设置开放权限')
  975. });
  976. // #endif
  977. // #ifdef MP-ALIPAY
  978. uni.chooseAddress({
  979. success: function(res) {
  980. success.call(this, res);
  981. },
  982. })
  983. // #endif
  984. }
  985. const isAuth = () => {
  986. const user = getStorage('userInfo');
  987. return user && user.id;
  988. }
  989. // 去掉字符串中的特殊字符和转义字符
  990. const excludeSpecial = (s) => {
  991. // 去掉转义字符
  992. // s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
  993. const pattern = /[`~!@#$^&*()=|{}':;',\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""' + - - _ % 。,、?\s]/g;
  994. if (s != undefined || s != null) {
  995. s = s.replace(pattern, "")
  996. }
  997. return s;
  998. }
  999. // 行为操作
  1000. const saveBehavior = (params, success) => {
  1001. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、海报
  1002. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知 13、素材 15、科室 16、海报 20、医生 21日历、探索 47评价
  1003. postRequest('/api/v3/save', params, data => {
  1004. success.call(this, data)
  1005. })
  1006. }
  1007. const getBehavior = (params, success) => {
  1008. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、海报
  1009. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知 13、素材 15、科室 16、海报 20、医生 21日历、探索 47评价
  1010. console.log('行为')
  1011. getRequest('/api/v3/get', params, data => {
  1012. success.call(this, data)
  1013. })
  1014. }
  1015. /**
  1016. * text:展示导航名
  1017. * iconPath:未点击显示图标
  1018. * selectedIconPath:选中图标
  1019. * pagePath:页面路径
  1020. * channel:匹配名
  1021. * click:点击事件
  1022. * index:排序
  1023. * show:是否显示
  1024. */
  1025. var selectedIconPath
  1026. if (header.appId == 'ZQ1VK5oc17I387E') {
  1027. selectedIconPath = '/static/pages/images/' + publics.iconSrc + 'yunyao_h.png'
  1028. } else {
  1029. selectedIconPath = '/static/pages/images/' + publics.iconSrc + 'home_h.png'
  1030. }
  1031. var tab = [{
  1032. text: "首页",
  1033. iconPath: '/static/pages/images/self/index.png',
  1034. selectedIconPath: '/static/pages/images/self/index_h.png',
  1035. pagePath: 'pages/index/index',
  1036. channel: 'home',
  1037. click: 'goHome',
  1038. index: 1,
  1039. show: true
  1040. },
  1041. {
  1042. text: "机柜",
  1043. iconPath: '/static/pages/images/self/machine.png',
  1044. selectedIconPath: '/static/pages/images/self/machine_h.png',
  1045. pagePath: 'pages/machine/machine',
  1046. click: 'goMachine',
  1047. channel: 'machine',
  1048. index: 2,
  1049. show: true
  1050. },
  1051. {
  1052. text: "电池",
  1053. iconPath: '/static/pages/images/self/cell.png',
  1054. selectedIconPath: '/static/pages/images/self/cell_h.png',
  1055. pagePath: 'pages/cell/cell',
  1056. click: 'goCell',
  1057. channel: 'cell',
  1058. index: 3,
  1059. show: true
  1060. },
  1061. {
  1062. text: "订单",
  1063. iconPath: '/static/pages/images/self/order.png',
  1064. selectedIconPath: '/static/pages/images/self/order_h.png',
  1065. pagePath: 'pages/order/order',
  1066. click: 'goOrder',
  1067. channel: 'order',
  1068. index: 5,
  1069. show: true
  1070. },
  1071. {
  1072. text: "我的",
  1073. iconPath: '/static/pages/images/self/user.png',
  1074. selectedIconPath: '/static/pages/images/self/user_h.png',
  1075. pagePath: 'pages/user/user',
  1076. click: 'goUser',
  1077. channel: 'user',
  1078. index: 6,
  1079. show: true
  1080. }
  1081. ]
  1082. module.exports = {
  1083. setStorage: setStorage,
  1084. getStorage: getStorage,
  1085. postRequest: postRequest,
  1086. postRequestLoding: postRequestLoding,
  1087. getRequest: getRequest,
  1088. putRequest: putRequest,
  1089. putRequestJson: putRequestJson,
  1090. msg: msg,
  1091. g: g,
  1092. p: p,
  1093. env: env,
  1094. send: send,
  1095. redirectIndex: redirectIndex,
  1096. saveImage: saveImage,
  1097. saveImageToPhotosAlbum: saveImageToPhotosAlbum,
  1098. uploadFile: uploadFile,
  1099. msgConfirm: msgConfirm,
  1100. getConfig: getConfig,
  1101. load: load,
  1102. isLogin: isLogin,
  1103. isLogins: isLogins,
  1104. payOrder: payOrder,
  1105. payOrders: payOrders,
  1106. getLocation: getLocation,
  1107. scopeAddress: scopeAddress,
  1108. isAuth: isAuth,
  1109. alertMsg: alertMsg,
  1110. getToken: getToken,
  1111. loadIng: loadIng,
  1112. removeStorage: removeStorage,
  1113. public: publics,
  1114. header: header,
  1115. // gettmplIds: gettmplIds,
  1116. excludeSpecial: excludeSpecial,
  1117. tab: tab,
  1118. msgConfirmText: msgConfirmText,
  1119. silenceLogin: silenceLogin,
  1120. getWXUserProfile: getWXUserProfile,
  1121. updataWxInfos: updataWxInfos,
  1122. saveBehavior: saveBehavior,
  1123. getBehavior: getBehavior
  1124. }