request.js 26 KB

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