request.js 25 KB

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