request.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. const app = getApp();
  2. let WebIM = (wx.WebIM = require("../hxChatSDK/utils/WebIM")["default"]);
  3. import JSEncrypt from '../utils/jsencrypt.js'
  4. const env = {
  5. NODE_ENV: 'prd',
  6. dev: {
  7. // apiUrl: 'https://rsapi.zhiqiyun.net'
  8. apiUrl: 'https://unwx.e-chinalife.com/hkttest',
  9. },
  10. prd: {
  11. apiUrl: 'https://unwx.e-chinalife.com/hktonline'
  12. },
  13. pota: {
  14. // apiUrl: 'http://192.168.110.98:8098'
  15. // apiUrl: 'http://192.168.110.180:8098' ,//f
  16. // apiUrl: 'http://192.168.110.182:8094' ,//z
  17. // apiUrl: 'http://192.168.110.176:8098' //y
  18. // apiUrl: 'http://192.168.110.117:8098'//l
  19. // apiUrl: 'http://192.168.110.216:8098'
  20. apiUrl: 'http://192.168.110.180:8098' ,//wjg
  21. }
  22. }
  23. var header = {
  24. 'content-type': 'application/x-www-form-urlencoded',
  25. 'appId': 'ZQhe229Ax49Pdpy', //中国人寿测试环境
  26. }
  27. /**小程序配置 */
  28. const publics = {
  29. picUrls: 'https://zhiqiyun.oss-cn-hangzhou.aliyuncs.com/static/', //图片地址
  30. 'copyrighttitle': '智企云', //底部版权公司名称
  31. 'copyrighttel': '400-698-5980', //底部版权服务电话
  32. 'copyrightlogo': 'zhiqiyun', //底部版权 logo 文件名
  33. 'yaomeng': 'https://cfwz.yaomengwang.cn/hunan?enterpriseCode=hnty&orderId=', //药盟接口地址
  34. 'storemanagers': 'https://websmall.zhiqiyun.com?appId=', //saas门店管理地址(智企云所有)
  35. 'channel': '', //处方中间渠道号 SAAS系统为空
  36. 'is_customer_pick_up': false,
  37. 'mapLBSKEY': 'CBRBZ-5SHKL-3NCPX-EP5MQ-B57M6-CWFUU',
  38. 'iconSrc': 'red/',
  39. 'titleTopBgColor': '#FFF',
  40. // 小程序ID wxe7bec198ecfe1162
  41. // 文章栏目code
  42. 'fxCode': 'faxian', //发现
  43. 'xbrmCode': 'xiaobairumen', //小白入门
  44. 'tbalCode': 'toubaoanli', //投保案例
  45. 'lpalCode': 'lipeianli', //理赔案例
  46. 'gfdtCode':'gfdt',//官方动态
  47. 'RSAKEY':"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDehEEc+q5s+m8c7fajkuzh5Cj/obdzngBZZmOHUcc3ck3oSfQQgs/bUoVCccJqbSSL4Z2+/BEq4wNeSeIH4nb26Euml2HhtFzVHej+of2r6NR+zB2DVbddoHJxcvb3kTC5KWPEZ6pN9aw4guMk2d78ZhWdfYSkmuNAebytSyc7ZQIDAQAB"
  48. }
  49. /*
  50. * 获取token
  51. */
  52. const getToken = (success) => {
  53. let token = getStorage('AUTH_TOKEN');
  54. if (token) {
  55. success.call(this, token);
  56. return false;
  57. } else {
  58. loadToken(data => {
  59. success.call(this, data);
  60. });
  61. }
  62. }
  63. const loadToken = (success) => {
  64. let that = this
  65. uni.request({
  66. url: env[env.NODE_ENV].apiUrl + '/api/access_token',
  67. data: {},
  68. method: 'get',
  69. header: header,
  70. success(json) {
  71. if (json.statusCode !== 200) return msg(json.data.message);
  72. if (json.data.code !== 0) return msg(json.data.msg)
  73. setStorage('AUTH_TOKEN', json.data.data);
  74. success.call(that, json.data.data);
  75. },
  76. fail() {
  77. msg('access_token 获取失败');
  78. }
  79. })
  80. }
  81. /**
  82. * 微信小程序静默授权登录
  83. * @param {*} parentId
  84. * @param {*} merchantId
  85. */
  86. const silenceLogin = (parentId, merchantId) => {
  87. // #ifdef MP-WEIXIN
  88. return new Promise((resolve, reject) => {
  89. if (!isLogins(false)) { //未登录
  90. uni.login({
  91. success(res) {
  92. console.log('静默授权登录>>>', res);
  93. let params = {
  94. code: res.code,
  95. parentId: parentId ? parentId : '',
  96. merchantId: merchantId ? merchantId : ''
  97. }
  98. postRequest('/api/v3/silence/login', params, data => {
  99. if (data.token) setStorage('AUTH_TOKEN', data.token);
  100. getRequest('/api/user/info', {}, suc => {
  101. data.isDistriUser = suc.isDistriUser;
  102. setStorage('userInfo', data);
  103. getIMToken(false);
  104. });
  105. resolve();
  106. });
  107. }
  108. });
  109. } else {
  110. initIMChat();
  111. resolve();
  112. }
  113. });
  114. // #endif
  115. }
  116. const getsysConfig = (upData)=>{
  117. // upData true刷新缓存系统参数
  118. return new Promise((r,j)=>{
  119. if(upData){
  120. getRequest('/api/other/config', {}, res => {
  121. setStorage('configRes', JSON.stringify(res));
  122. r(res)
  123. });
  124. }else{
  125. if(getStorage('configRes')) r(JSON.parse(getStorage('configRes')))
  126. else{
  127. getRequest('/api/other/config', {}, res => {
  128. setStorage('configRes', JSON.stringify(res));
  129. r(res)
  130. });
  131. }
  132. }
  133. })
  134. }
  135. /**
  136. * 初始化 IMsdk
  137. */
  138. const initIMChat = () => {
  139. var user = getStorage('userInfo');
  140. if (user) {
  141. var imToken = getStorage('imToken');
  142. if (imToken) {
  143. //初始化IM
  144. uni.setStorage({
  145. key: "myUsername",
  146. data: 'RS_' + user.id
  147. });
  148. getApp().globalData.conn.open({
  149. apiUrl: WebIM.config.apiURL,
  150. user: 'RS_' + user.id,
  151. pwd: getStorage('imToken'),
  152. accessToken: getStorage('imToken'),
  153. appKey: WebIM.config.appkey
  154. });
  155. } else {
  156. getRequest('/api/im/msg/userToken', {}, suc => {
  157. setStorage('imToken', suc);
  158. uni.setStorage({
  159. key: "myUsername",
  160. data: 'RS_' + user.id
  161. });
  162. getApp().globalData.conn.open({
  163. apiUrl: WebIM.config.apiURL,
  164. user: 'RS_' + user.id,
  165. pwd: getStorage('imToken'),
  166. accessToken: getStorage('imToken'),
  167. appKey: WebIM.config.appkey
  168. });
  169. });
  170. }
  171. }
  172. }
  173. const getIMToken = (isUpdatToken) => {
  174. var user = getStorage('userInfo');
  175. getRequest('/api/im/msg/userToken', {}, suc => {
  176. setStorage('imToken', suc);
  177. if (isUpdatToken) {
  178. //更新token
  179. uni.setStorage({
  180. key: "myUsername",
  181. data: 'RS_' + user.id
  182. });
  183. getApp().globalData.conn.open({
  184. user: 'RS_' + user.id,
  185. accessToken: getStorage('imToken'),
  186. });
  187. getApp().globalData.onLoginSuccess(uni.getStorageSync("myUsername"));
  188. } else {
  189. initIMChat();
  190. }
  191. }, true);
  192. }
  193. /**
  194. * 跳转进入消息列表
  195. */
  196. const jumpConversation = () => {
  197. if (!isLogins(true)) { //未登录
  198. return;
  199. }
  200. getIMToken(true);
  201. }
  202. const getWXUserProfile = (suc) => {
  203. let that = this;
  204. var sessionKey = '';
  205. var openid = '';
  206. getApp().globalData.getCheckSessoin(json => {
  207. sessionKey = json.session_key;
  208. openid = json.openid;
  209. });
  210. try {
  211. uni.getUserProfile({
  212. desc: '完善信息',
  213. success: res => {
  214. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  215. },
  216. fail: res => {
  217. console.log('授权失败数据>>>', res);
  218. msg(res);
  219. }
  220. });
  221. } catch (e) {
  222. // 老版
  223. wx.getUserInfo({
  224. success: function(ress) {
  225. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  226. }
  227. });
  228. }
  229. }
  230. /**
  231. * 保存微信头像
  232. */
  233. const updataWxInfos = (iv, encryptedData, sessionKey, openid, suc) => {
  234. if (!encryptedData || !iv) return false;
  235. let _params = {
  236. sessionKey: sessionKey,
  237. openId: openid,
  238. iv: iv,
  239. encryptedData: encryptedData
  240. };
  241. postRequest('/api/user/weixin/update', _params, json => {
  242. setStorage('userInfo', json);
  243. suc.call();
  244. });
  245. }
  246. const clearValueEmpty = (data) => {
  247. let keyValue = {};
  248. for (let key in data) {
  249. let value = typeof data[key];
  250. if (value == 'string' && value) {
  251. if (data[key] != 'undefined' && data[key] != " " && data[key] != undefined && data[key] != null) {
  252. keyValue[key] = data[key];
  253. }
  254. // keyValue[key] = data[key];
  255. } else if (value == 'number' && value != null) {
  256. keyValue[key] = data[key];
  257. } else if (value == 'boolean') {
  258. keyValue[key] = data[key];
  259. } else {
  260. if (data[key]) keyValue[key] = data[key];
  261. }
  262. }
  263. return keyValue;
  264. }
  265. /**
  266. * 公共请求
  267. *
  268. * @param {*} url 请求url
  269. * @param {*} data 请求参数
  270. * @param {*} method 请求方法
  271. * @param {*} success 成功函数
  272. * @param {*} isLoad 是否显示加载提示
  273. */
  274. const baseRequest = (url, data, method, success, isLoad) => {
  275. let postData = data
  276. getToken(token => {
  277. if (!isLoad) uni.showNavigationBarLoading();
  278. header.authorityToken = token;
  279. // if (url.indexOf('/v3') != -1 || url.indexOf('/paper/create') != -1) {
  280. // header['content-type'] = 'application/json;charset=UTF-8'
  281. // } else {
  282. header['content-type'] = 'application/json;charset=UTF-8'
  283. // }
  284. if(url=='/api/user/userInfoById'||url=='/api/visiting/card/info'){
  285. if(data.id){
  286. let encrypt = new JSEncrypt()
  287. encrypt.setPublicKey(publics.RSAKEY)
  288. data.id = encrypt.encrypt(data.id)
  289. }
  290. if(data.userId){
  291. let encrypt = new JSEncrypt()
  292. encrypt.setPublicKey(publics.RSAKEY)
  293. data.userId = encrypt.encrypt(data.userId)
  294. }
  295. }
  296. uni.request({
  297. url: env[env.NODE_ENV].apiUrl + url,
  298. data: clearValueEmpty(data),
  299. method: method,
  300. header: header,
  301. success(json) {
  302. if (!isLoad) uni.hideNavigationBarLoading();
  303. if (json.statusCode !== 200) return msg(json.data.message);
  304. if (json.data.code === 10021 || json.data.code === 10020) {
  305. // console.log("第二次执行")
  306. removeStorage('AUTH_TOKEN');
  307. removeStorage('userInfo');
  308. return redirectIndex();
  309. }
  310. if (json.data.code !== 0) {
  311. return msg(json.data.msg)
  312. }
  313. // console.log("第二次执行")
  314. let data = json.data.data;
  315. if (typeof data === 'string' && data.indexOf("{") === 0) {
  316. data = JSON.parse(data);
  317. }
  318. try{
  319. // 用户授权手机号日志
  320. if(url=='/api/weixin/mobile'){
  321. let sys = uni.getSystemInfoSync()
  322. let dataP = {
  323. type:3,
  324. model: sys.model,
  325. os: sys.osName,
  326. osVersion: sys.osVersion
  327. }
  328. postRequest('/api/log/authorize/save',dataP,res=>{
  329. })
  330. }
  331. // 用户授权昵称头像日志
  332. if(url=='/api/user/save' && (postData.avatar || postData.nickName)){
  333. let sys = uni.getSystemInfoSync()
  334. let dataP = {
  335. type:'',
  336. model: sys.model,
  337. os: sys.osName,
  338. osVersion: sys.osVersion
  339. }
  340. if(postData.avatar&&postData.nickName) dataP.type = '1,2'
  341. else if(postData.nickName) dataP.type = '1'
  342. else if(postData.avatar) dataP.type = '2'
  343. postRequest('/api/log/authorize/save',dataP,res=>{
  344. })
  345. }
  346. }catch(e){
  347. //TODO handle the exception
  348. }
  349. success.call(this, data);
  350. },
  351. fail() {
  352. if (!isLoad) uni.hideNavigationBarLoading();
  353. },
  354. // complete(){
  355. // success.call(this, data);
  356. // },
  357. })
  358. })
  359. }
  360. const loadIng = (msg) => {
  361. uni.showLoading({
  362. title: msg,
  363. mask: true
  364. })
  365. }
  366. /**POST请求 */
  367. const postRequest = (url, data, success, isLoad) => {
  368. header['content-type'] = 'application/json;charset=UTF-8'
  369. baseRequest(url, data, 'post', success, isLoad);
  370. }
  371. const postRequestLoding = (url, data, success, isLoding) => {
  372. header['content-type'] = 'application/json;charset=UTF-8'
  373. baseRequest(url, data, 'post', success, false, isLoding);
  374. }
  375. /**GET请求 */
  376. const getRequest = (url, data, success, isLoad) => {
  377. header['content-type'] = 'application/json;charset=UTF-8'
  378. baseRequest(url, data, 'get', success, isLoad);
  379. }
  380. /**Put请求 */
  381. const putRequest = (url, data, success, isLoad) => {
  382. header['content-type'] = 'application/json;charset=UTF-8'
  383. baseRequest(url, data, 'put', success, isLoad)
  384. }
  385. const putRequestJson = (url, data, success, isLoad) => {
  386. header['content-type'] = 'application/json;charset=UTF-8'
  387. baseRequest(url, data, 'put', success, isLoad)
  388. }
  389. /**
  390. * 上传文件
  391. * @param {*} url 请求url
  392. * @param {*} data 携带数据
  393. * @param {*} success 请求成功函数
  394. */
  395. const uploadFile = (url, data, success) => {
  396. // console.log(env[env.NODE_ENV].apiUrl + url, data)
  397. getToken(token => {
  398. uni.uploadFile({
  399. url: env[env.NODE_ENV].apiUrl + url,
  400. filePath: data,
  401. name: 'file',
  402. header: {
  403. 'appId': header['appId'],
  404. 'authorityToken': token
  405. },
  406. success(res) {
  407. if (res.statusCode !== 200) return msg('文件上传失败');
  408. let data = JSON.parse(res.data);
  409. if (data.code !== 0) return msg(data.msg);
  410. success.call(this, data.data);
  411. },
  412. })
  413. })
  414. }
  415. /**弹窗 */
  416. const msg = (title, success) => {
  417. if (title) {
  418. uni.showToast({
  419. title: title,
  420. icon: 'none',
  421. success() {
  422. if (success) success.call(this);
  423. }
  424. });
  425. }
  426. return false;
  427. }
  428. const msgConfirm = (msg, success, cancel) => {
  429. uni.showModal({
  430. title: '提示',
  431. content: msg,
  432. success(res) {
  433. if (res.confirm) {
  434. success.call(this);
  435. } else {
  436. if (cancel) cancel.call(this);
  437. }
  438. }
  439. })
  440. }
  441. const msgConfirmText = (msg, confirmText, success, cancel) => {
  442. uni.showModal({
  443. title: '提示',
  444. content: msg,
  445. confirmText: confirmText,
  446. success(res) {
  447. if (res.confirm) {
  448. success.call(this);
  449. } else {
  450. if (cancel) cancel.call(this);
  451. }
  452. }
  453. })
  454. }
  455. const alertMsg = (msg, success) => {
  456. uni.showModal({
  457. title: '提示',
  458. content: msg,
  459. showCancel: false,
  460. success(res) {
  461. if (success) success.call(this);
  462. }
  463. })
  464. return false;
  465. }
  466. const load = msg => {
  467. uni.showLoading({
  468. title: msg,
  469. mask: true
  470. })
  471. }
  472. const g = (url, success, isLoad) => {
  473. getRequest(url, {}, success, isLoad)
  474. }
  475. const p = (url, success, isLoad) => {
  476. postRequest(url, {}, success, isLoad)
  477. }
  478. const send = (url, mobile, success, error) => {
  479. if (!mobile) {
  480. return msg('手机号码不能为空', error);
  481. }
  482. postRequest(url, {
  483. phone: mobile
  484. }, json => {
  485. if (json.code !== 0) {
  486. return msg(json.msg, error);
  487. }
  488. let time = 60;
  489. const initTime = setInterval(() => {
  490. time--;
  491. if (time > 0) {
  492. success.call(this, time + '秒后获取');
  493. } else {
  494. clearInterval(initTime);
  495. success.call(this, '获取验证码');
  496. }
  497. }, 1000)
  498. })
  499. }
  500. // 登陆拦截
  501. const redirectIndex = (params) => {
  502. // uni.clearStorageSync();
  503. // console.log(params)
  504. var pages = getCurrentPages();
  505. // console.log("pages》》》》》登陆拦截", pages);
  506. let currentUrl;
  507. if (pages) {
  508. var currentPage = pages[pages.length - 1];
  509. if (currentPage) {
  510. currentUrl = currentPage.route;
  511. let query = currentPage.data.query;
  512. if (query) {
  513. for (let key in query) {
  514. const fo = key + '=' + query[key];
  515. currentUrl += currentUrl.indexOf('?') > -1 ? '&' + fo : '?' + fo;
  516. }
  517. }
  518. if (currentUrl) {
  519. console.log('currentUrl》》》》》', currentUrl);
  520. setStorage("REDIRECT_URL", '/' + currentUrl)
  521. if (currentUrl == 'pages/authorize/authorize') {
  522. console.log('当前已经处于登录页面,不在向下执行跳转');
  523. return
  524. }
  525. }
  526. }
  527. }
  528. let redirectUrl = '/pages/authorize/authorize' + (params ? params : '');
  529. // console.log("登陆拦截")
  530. uni.navigateTo({
  531. url: redirectUrl
  532. })
  533. return false;
  534. }
  535. const saveImage = (url) => {
  536. uni.saveImageToPhotosAlbum({
  537. filePath: url,
  538. success() {
  539. msg('图片保存成功');
  540. }
  541. })
  542. }
  543. const saveImageToPhotosAlbum = (url) => {
  544. if (!url) return msg('小程序码不存在');
  545. uni.getImageInfo({
  546. src: url,
  547. success(json) {
  548. // uni.getSetting({
  549. // success(res) {
  550. // if (!res.authSetting['scope.writePhotosAlbum']) {
  551. // uni.authorize({
  552. // scope: 'scope.writePhotosAlbum',
  553. // success() {
  554. // saveImage(json.path);
  555. // }
  556. // })
  557. // } else {
  558. // saveImage(json.path);
  559. // }
  560. // }
  561. // })
  562. }
  563. })
  564. }
  565. const getConfig = (config, id) => {
  566. let con = {};
  567. config.info.forEach(inf => {
  568. // console.log(inf.version, inf.version ? inf.version.indexOf(id) : '-')
  569. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  570. con[inf.setName] = inf.setDefault;
  571. });
  572. config.card.forEach(inf => {
  573. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  574. con[inf.setName] = inf.setDefault;
  575. });
  576. config.realTime.forEach(inf => {
  577. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  578. con[inf.setName] = inf.setDefault;
  579. });
  580. return con;
  581. }
  582. const isLogin = async (error) => {
  583. let userInfo = getStorage('userInfo');
  584. if (!userInfo || !userInfo.id) {
  585. if (error) return false;
  586. // console.log("第一次执行")
  587. return redirectIndex();
  588. }
  589. return true;
  590. }
  591. //检测是否登录
  592. const isLogins = (isGoLogin) => {
  593. let userInfo = getStorage('userInfo');
  594. if (!userInfo) {
  595. if (isGoLogin) {
  596. redirectIndex();
  597. }
  598. return false;
  599. }
  600. return true;
  601. }
  602. const setStorage = (key, value) => {
  603. uni.setStorageSync(env.NODE_ENV + "_" + key, value)
  604. }
  605. const getStorage = (key) => {
  606. return uni.getStorageSync(env.NODE_ENV + "_" + key)
  607. }
  608. const removeStorage = (key) => {
  609. return uni.removeStorageSync(env.NODE_ENV + "_" + key);
  610. }
  611. /**
  612. * 缓存模板id
  613. */
  614. // const gettmplIds = () => {
  615. // getRequest('/api/wxmes/querylist', {}, res => {
  616. // setStorage('tmplIds', res)
  617. // console.log(res)
  618. // })
  619. // }
  620. //微信支付订单公共方法
  621. const payOrder = (id, success) => {
  622. var loctionAddressMap = getStorage('loctionAddressMap');
  623. var datas = {
  624. id: id
  625. };
  626. if (loctionAddressMap) {
  627. datas.province = loctionAddressMap.province;
  628. datas.city = loctionAddressMap.city;
  629. };
  630. if (getStorage('isVideoScene')) {
  631. uni.checkBeforeAddOrder({
  632. success(res) {
  633. console.log('下单前置检查', res)
  634. let data = res.data;
  635. data.requiredFundType = 0;
  636. datas.sceneCheckStr = JSON.stringify(data);
  637. wexinPay(datas, success)
  638. },
  639. })
  640. } else {
  641. wexinPay(datas, success)
  642. }
  643. }
  644. //支付中或已支付
  645. const payStatus = (json, id, success) => {
  646. if (json && json.code === 115) {
  647. setTimeout(res => {
  648. uni.hideLoading();
  649. isShowLoading = false;
  650. uni.showModal({
  651. title: '提示',
  652. content: '当前订单正在支付或30秒后重试',
  653. confirmText: '我知道了',
  654. showCancel: false,
  655. success(res) {}
  656. })
  657. }, 3000)
  658. return false;
  659. }
  660. if (json.type === 2 || json.type === 3) {
  661. //调用后台判断订单是否支付成功
  662. let i = 0;
  663. let timer = setInterval(() => {
  664. postRequest('/api/order/check', {
  665. id: id
  666. }, res => {
  667. if (res.isSuccess || i === 4) {
  668. clearInterval(timer);
  669. success.call(this, res.isSuccess);
  670. } else {
  671. i++;
  672. }
  673. }, true)
  674. }, 200);
  675. // success.call(this);
  676. return false;
  677. }
  678. }
  679. // 支付成功回调
  680. const paySuccess = (id, success) => {
  681. console.log('支付成功')
  682. let i = 0;
  683. let timer = setInterval(() => {
  684. postRequest('/api/order/check', {
  685. id: id
  686. }, res => {
  687. console.log(i)
  688. if (res.isSuccess || i === 5) {
  689. clearInterval(timer);
  690. success.call(this, res.isSuccess);
  691. } else {
  692. i++;
  693. }
  694. }, true)
  695. }, 500);
  696. }
  697. // 支付失败回调
  698. const payFail = (id) => {
  699. console.log('支付失败')
  700. postRequest('/api/order/cancelPay', {
  701. id: id
  702. }, data => {
  703. uni.redirectTo({
  704. url: '/mine/orderDet/orderDet?id=' + id,
  705. })
  706. })
  707. }
  708. const wexinPay = (datas, success) => {
  709. let isShowLoading = false;
  710. if (!isShowLoading) {
  711. loadIng('加载中');
  712. // console.log('加载中')
  713. isShowLoading = true;
  714. }
  715. // #ifdef MP-WEIXIN
  716. postRequest('/api/order/weixin/pay', datas, json => {
  717. // console.log('json',json)
  718. payStatus(json, datas.id, success)
  719. const sceneCheckStr = datas.sceneCheckStr;
  720. if (sceneCheckStr && JSON.parse(sceneCheckStr).requiredFundType === 1) {
  721. console.log('拉起收银台')
  722. wx.requestOrderPayment({
  723. timeStamp: json.timeStamp,
  724. nonceStr: json.nonceStr,
  725. package: json.packages,
  726. signType: json.signType,
  727. orderInfo: json.orderInfo,
  728. paySign: json.sign,
  729. success: function() {
  730. paySuccess(datas.id, success)
  731. },
  732. fail: function(res) {
  733. console.log(res);
  734. payFail(datas.id)
  735. }
  736. })
  737. } else {
  738. uni.requestPayment({
  739. timeStamp: json.timeStamp,
  740. nonceStr: json.nonceStr,
  741. package: json.packages,
  742. signType: json.signType,
  743. paySign: json.sign,
  744. success: function() {
  745. paySuccess(datas.id, success)
  746. },
  747. fail: function(res) {
  748. console.log(res);
  749. payFail(datas.id)
  750. }
  751. })
  752. }
  753. if (isShowLoading) {
  754. uni.hideLoading();
  755. isShowLoading = false;
  756. }
  757. })
  758. // #endif
  759. // #ifdef MP-ALIPAY
  760. datas.payChannel = 'alipay_lite ';
  761. postRequest('/api/order/weixin/pay', datas, json => {
  762. // console.log(json)
  763. if (json.type === 2 || json.type === 3) {
  764. //调用后台判断订单是否支付成功
  765. let i = 0;
  766. let timer = setInterval(() => {
  767. postRequest('/api/order/check', {
  768. id: id
  769. }, res => {
  770. if (res.isSuccess || i === 4) {
  771. clearInterval(timer);
  772. success.call(this, res.isSuccess);
  773. } else {
  774. i++;
  775. }
  776. }, true)
  777. }, 200);
  778. // success.call(this);
  779. return false;
  780. }
  781. uni.requestPayment({
  782. provider: 'alipay',
  783. orderInfo: json.packages,
  784. success: function() {
  785. paySuccess(datas.id, success)
  786. },
  787. fail: function(res) {
  788. payFail(datas.id)
  789. console.log(res);
  790. }
  791. })
  792. if (isShowLoading) {
  793. uni.hideLoading();
  794. isShowLoading = false;
  795. }
  796. })
  797. // #endif
  798. }
  799. //微信支付订单公共方法
  800. const payOrdersss = (id, success) => {
  801. let isShowLoading = false;
  802. if (!isShowLoading) {
  803. loadIng('加载中');
  804. // console.log('加载中')
  805. isShowLoading = true;
  806. }
  807. var loctionAddressMap = getStorage('loctionAddressMap');
  808. var datas = {
  809. id: id
  810. };
  811. if (loctionAddressMap) {
  812. datas.province = loctionAddressMap.province;
  813. datas.city = loctionAddressMap.city;
  814. };
  815. // #ifdef MP-WEIXIN
  816. postRequest('/api/order/weixin/pay', datas, json => {
  817. // console.log(json)
  818. if (json.type === 2 || json.type === 3) {
  819. //调用后台判断订单是否支付成功
  820. let i = 0;
  821. let timer = setInterval(() => {
  822. postRequest('/api/order/check', {
  823. id: id
  824. }, res => {
  825. if (res.isSuccess || i === 4) {
  826. clearInterval(timer);
  827. success.call(this, res.isSuccess);
  828. } else {
  829. i++;
  830. }
  831. }, true)
  832. }, 200);
  833. // success.call(this);
  834. return false;
  835. }
  836. uni.requestPayment({
  837. timeStamp: json.timeStamp,
  838. nonceStr: json.nonceStr,
  839. package: json.packages,
  840. signType: json.signType,
  841. paySign: json.sign,
  842. success: function() {
  843. console.log('支付成功')
  844. let i = 0;
  845. let timer = setInterval(() => {
  846. postRequest('/api/order/check', {
  847. id: id
  848. }, res => {
  849. console.log(i)
  850. if (res.isSuccess || i === 5) {
  851. clearInterval(timer);
  852. success.call(this, res.isSuccess);
  853. } else {
  854. i++;
  855. }
  856. }, true)
  857. }, 500);
  858. },
  859. fail: function(res) {
  860. console.log('支付失败')
  861. postRequest('/api/order/cancelPay', {
  862. id: id
  863. }, data => {
  864. uni.redirectTo({
  865. url: '/mine/orderDet/orderDet?id=' + id,
  866. })
  867. })
  868. console.log(res);
  869. }
  870. })
  871. if (isShowLoading) {
  872. uni.hideLoading();
  873. isShowLoading = false;
  874. }
  875. })
  876. // #endif
  877. // #ifdef MP-ALIPAY
  878. datas.payChannel = 'alipay_lite ';
  879. postRequest('/api/order/weixin/pay', datas, json => {
  880. // console.log(json)
  881. if (json.type === 2 || json.type === 3) {
  882. //调用后台判断订单是否支付成功
  883. let i = 0;
  884. let timer = setInterval(() => {
  885. postRequest('/api/order/check', {
  886. id: id
  887. }, res => {
  888. if (res.isSuccess || i === 4) {
  889. clearInterval(timer);
  890. success.call(this, res.isSuccess);
  891. } else {
  892. i++;
  893. }
  894. }, true)
  895. }, 200);
  896. // success.call(this);
  897. return false;
  898. }
  899. uni.requestPayment({
  900. provider: 'alipay',
  901. orderInfo: json.packages,
  902. success: function() {
  903. console.log('支付成功')
  904. let i = 0;
  905. let timer = setInterval(() => {
  906. postRequest('/api/order/check', {
  907. id: id
  908. }, res => {
  909. console.log(i)
  910. if (res.isSuccess || i === 5) {
  911. clearInterval(timer);
  912. success.call(this, res.isSuccess);
  913. } else {
  914. i++;
  915. }
  916. }, true)
  917. }, 500);
  918. },
  919. fail: function(res) {
  920. console.log('支付失败')
  921. postRequest('/api/order/cancelPay', {
  922. id: id
  923. }, data => {
  924. uni.redirectTo({
  925. url: '/mine/orderDet/orderDet?id=' + id,
  926. })
  927. })
  928. console.log(res);
  929. }
  930. })
  931. if (isShowLoading) {
  932. uni.hideLoading();
  933. isShowLoading = false;
  934. }
  935. })
  936. // #endif
  937. }
  938. //微信支付订单公共方法 ==活动
  939. const payOrders = (id, success, type) => {
  940. let isShowLoading = false;
  941. if (!isShowLoading) {
  942. loadIng('加载中');
  943. // console.log('加载中')
  944. isShowLoading = true;
  945. }
  946. postRequest('/api/order/weixin/pay', {
  947. id: id
  948. }, json => {
  949. // console.log(json)
  950. payStatus(json, id, success)
  951. uni.requestPayment({
  952. timeStamp: json.timeStamp,
  953. nonceStr: json.nonceStr,
  954. package: json.packages,
  955. signType: json.signType,
  956. paySign: json.sign,
  957. success: function() {
  958. paySuccess(id, success)
  959. },
  960. fail: function(res) {
  961. console.log('支付失败')
  962. postRequest('/api/order/cancelPay', {
  963. id: id
  964. }, data => {
  965. uni.redirectTo({
  966. url: '/mine/activity/activity',
  967. })
  968. })
  969. console.log(res);
  970. }
  971. })
  972. if (isShowLoading) {
  973. uni.hideLoading();
  974. isShowLoading = false;
  975. }
  976. })
  977. }
  978. const authSetting = (authority, success, error) => {
  979. // console.log(authority,success)
  980. // #ifndef H5
  981. uni.getSetting({
  982. success(res) {
  983. if (res.authSetting[authority]) {
  984. success.call(this);
  985. return false;
  986. }
  987. uni.authorize({
  988. scope: authority,
  989. success() {
  990. success.call(this);
  991. },
  992. fail: function(res) {
  993. error.call(this);
  994. }
  995. })
  996. }
  997. })
  998. // #endif
  999. }
  1000. const getLocation = (suss) => {
  1001. // #ifdef MP-WEIXIN
  1002. authSetting('scope.userLocation', () => {
  1003. // load('定位中…');
  1004. uni.getLocation({
  1005. type: 'gcj02',
  1006. // isHighAccuracy: true,
  1007. success: function(res) {
  1008. // uni.hideLoading();
  1009. console.log(res, "789")
  1010. suss.call(this, res);
  1011. },
  1012. fail: function(res) {
  1013. console.log("调用失败", res)
  1014. suss.call(this, 2)
  1015. // uni.hideLoading();
  1016. }
  1017. })
  1018. }, () => {
  1019. // uni.hideLoading();
  1020. console.log("取消")
  1021. suss.call(this, 1)
  1022. });
  1023. // #endif
  1024. // #ifdef MP-ALIPAY
  1025. uni.getLocation({
  1026. type: 'gcj02',
  1027. // isHighAccuracy: true,
  1028. success: function(res) {
  1029. // uni.hideLoading();
  1030. console.log(res, "789")
  1031. suss.call(this, res);
  1032. },
  1033. fail: function(res) {
  1034. console.log("调用失败", res)
  1035. suss.call(this, 2)
  1036. // uni.hideLoading();
  1037. }
  1038. })
  1039. // #endif
  1040. // #ifdef H5
  1041. uni.getLocation({
  1042. type: 'gcj02',
  1043. isHighAccuracy: true,
  1044. success: function(res) {
  1045. // uni.hideLoading();
  1046. console.log(res)
  1047. suss.call(this, res, "789");
  1048. },
  1049. fail: function() {
  1050. console.log("调用失败", res)
  1051. suss.call(this, 2)
  1052. // uni.hideLoading();
  1053. }
  1054. })
  1055. // #endif
  1056. }
  1057. const scopeAddress = (success) => {
  1058. // #ifdef MP-WEIXIN
  1059. authSetting('scope.address', () => {
  1060. uni.chooseAddress({
  1061. success: function(res) {
  1062. success.call(this, res);
  1063. },
  1064. })
  1065. }, () => {
  1066. msg('未设置开放权限')
  1067. });
  1068. // #endif
  1069. // #ifdef MP-ALIPAY
  1070. uni.chooseAddress({
  1071. success: function(res) {
  1072. success.call(this, res);
  1073. },
  1074. })
  1075. // #endif
  1076. }
  1077. const isAuth = () => {
  1078. const user = getStorage('userInfo');
  1079. return user && user.id;
  1080. }
  1081. // 去掉字符串中的特殊字符和转义字符
  1082. const excludeSpecial = (s) => {
  1083. // 去掉转义字符
  1084. // s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
  1085. const pattern = /[`~!@#$^&*()=|{}':;',\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""' + - - _ % 。,、?\s]/g;
  1086. if (s != undefined || s != null) {
  1087. s = s.replace(pattern, "")
  1088. }
  1089. return s;
  1090. }
  1091. // 数字单位格式化
  1092. function unitConverter(num) {
  1093. // text 数字+单位 number格式化后的数字 unit单位
  1094. console.log(num)
  1095. if (!num || isNaN(num)) {
  1096. return {
  1097. text: 0,
  1098. number: 0,
  1099. unit: ''
  1100. }
  1101. }
  1102. num = Number(num)
  1103. if (Math.abs(num) > 100000000) {
  1104. return {
  1105. text: (num / 100000000).toFixed(2) + '亿',
  1106. number: (num / 100000000).toFixed(2),
  1107. unit: '亿'
  1108. }
  1109. } else if (Math.abs(num) > 10000) {
  1110. return {
  1111. text: (num / 10000).toFixed(2) + '万',
  1112. number: (num / 10000).toFixed(2),
  1113. unit: '万'
  1114. }
  1115. } else {
  1116. return {
  1117. text: num,
  1118. number: num,
  1119. unit: ''
  1120. }
  1121. }
  1122. }
  1123. // 行为操作
  1124. const saveBehavior = (params, success) => {
  1125. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、海报
  1126. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知 13、素材 15、科室 16、海报 20、医生 21日历、探索 47评价
  1127. postRequest('/api/v3/behavior/save', params, data => {
  1128. success.call(this, data)
  1129. })
  1130. }
  1131. const getBehavior = (params, success) => {
  1132. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、海报
  1133. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知 13、素材 15、科室 16、海报 20、医生 21日历、探索 47评价
  1134. console.log('行为')
  1135. getRequest('/api/v3/behavior/get', params, data => {
  1136. success.call(this, data)
  1137. })
  1138. }
  1139. /**
  1140. * @Description:
  1141. * @author: xionghaojie
  1142. * @param bindId 操作对象ID(如果是页面不需要传)
  1143. * @param type 用户行为操作对象(1.名片 2.活动 3.文章 4.社区页面 5.海报 6.点击发现页面 7.点击首页页面 8.点击我的页面 10.聊一聊 11.电话 12.微信 13.通讯录 14.评价名片)
  1144. * @param behavior 用户行为(1.关注 2.收藏 3.点赞 4.浏览 5.确认 6.分享 7.发布 8.评价 9.投票 10.点击 11.取消关注 12.取消点赞 13.评价 14.保存通讯录 15.聊一聊 16.加入)
  1145. * @param content 用户行为内容(非必填)
  1146. * @return
  1147. * @createTime: 2023-10-16 10:48:32
  1148. */
  1149. const saveBehaviorNew = async (bindId,type,behavior,content, success) => {
  1150. let params = {
  1151. uid:'',
  1152. bindId:bindId,
  1153. behavior:behavior,
  1154. type:type,
  1155. ip:'',//ip
  1156. manufacturer:'',
  1157. model:'',//设备型号
  1158. os:'',//操作系统
  1159. osVersion:'',//操作系统版本
  1160. }
  1161. await new Promise((resolve,reject)=>{
  1162. uni.getSystemInfoAsync({
  1163. success(res) {
  1164. console.log(res)
  1165. params.manufacturer = res.brand //手机牌子
  1166. params.model = res.model
  1167. params.os = res.system
  1168. params.osVersion = res.version
  1169. resolve()
  1170. },
  1171. fail(error){
  1172. console.log('getSystemInfoSync error',error)
  1173. resolve()
  1174. }
  1175. })
  1176. })
  1177. params.uid = getStorage('userInfo')?getStorage('userInfo').id:''
  1178. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、海报
  1179. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知 13、素材 15、科室 16、海报 20、医生 21日历、探索 47评价
  1180. postRequest('/api/behaviorNew/save', params, data => {
  1181. success.call(this, data)
  1182. })
  1183. }
  1184. /**
  1185. * 跳入聊天
  1186. * @param {Object} userId 对方用户ID
  1187. * @param {Object} avatar 对方用户头像
  1188. * @param {Object} realName 对方用户名称
  1189. */
  1190. const jumpSingleChatRoom = function(userId, avatar, realName) {
  1191. var my = uni.getStorageSync("myUsername");
  1192. var nameList = {
  1193. myName: my,
  1194. your: 'rs_' + userId
  1195. };
  1196. uni.setStorage({
  1197. key: 'imUserInfo_' + nameList.your,
  1198. data: {
  1199. avatar: avatar,
  1200. id: userId,
  1201. nickName: realName
  1202. }
  1203. });
  1204. uni.navigateTo({
  1205. url: "/chat/chatroom/chatroom?username=" + JSON.stringify(nameList)
  1206. });
  1207. }
  1208. /**
  1209. * text:展示导航名
  1210. * iconPath:未点击显示图标
  1211. * selectedIconPath:选中图标
  1212. * pagePath:页面路径
  1213. * channel:匹配名
  1214. * click:点击事件
  1215. * index:排序
  1216. * show:是否显示
  1217. */
  1218. var selectedIconPath
  1219. if (header.appId == 'ZQ1VK5oc17I387E') {
  1220. selectedIconPath = '/static/pages/images/' + publics.iconSrc + 'yunyao_h.png'
  1221. } else {
  1222. selectedIconPath = '/static/pages/images/' + publics.iconSrc + 'home_h.png'
  1223. }
  1224. var tab = [{
  1225. text: "首页",
  1226. iconPath: "/static/pages/images/home.png",
  1227. selectedIconPath: '/static/pages/images/home_h.png',
  1228. pagePath: 'pages/index/index',
  1229. channel: 'home',
  1230. click: 'goHome',
  1231. index: 1,
  1232. show: true
  1233. },
  1234. {
  1235. text: "活动",
  1236. iconPath: "/static/pages/images/activity.png",
  1237. selectedIconPath: '/static/pages/images/activity_h.png',
  1238. pagePath: 'pages/matchList/index',
  1239. click: 'goMatch',
  1240. channel: 'match',
  1241. index: 2,
  1242. show: true
  1243. },
  1244. {
  1245. text: "社区",
  1246. iconPath: "/static/pages/images/interactive.png",
  1247. selectedIconPath: '/static/pages/images/interactive_h.png',
  1248. pagePath: 'pages/interactive/index',
  1249. click: 'goInteractive',
  1250. channel: 'interactive',
  1251. index: 3,
  1252. show: true
  1253. },
  1254. {
  1255. text: "发现",
  1256. iconPath: "/static/pages/images/found.png",
  1257. selectedIconPath: '/static/pages/images/found_h.png',
  1258. pagePath: 'pages/found/found',
  1259. click: 'goFound',
  1260. channel: 'found',
  1261. index: 3,
  1262. show: true
  1263. },
  1264. {
  1265. text: "我的",
  1266. iconPath: '/static/pages/images/user.png',
  1267. selectedIconPath: '/static/pages/images/user_h.png',
  1268. pagePath: 'pages/user/user',
  1269. click: 'goUser',
  1270. channel: 'user',
  1271. index: 6,
  1272. show: true
  1273. }
  1274. ]
  1275. module.exports = {
  1276. setStorage: setStorage,
  1277. getStorage: getStorage,
  1278. postRequest: postRequest,
  1279. postRequestLoding: postRequestLoding,
  1280. getRequest: getRequest,
  1281. putRequest: putRequest,
  1282. putRequestJson: putRequestJson,
  1283. msg: msg,
  1284. g: g,
  1285. p: p,
  1286. env: env,
  1287. send: send,
  1288. redirectIndex: redirectIndex,
  1289. saveImage: saveImage,
  1290. saveImageToPhotosAlbum: saveImageToPhotosAlbum,
  1291. uploadFile: uploadFile,
  1292. msgConfirm: msgConfirm,
  1293. getConfig: getConfig,
  1294. load: load,
  1295. isLogin: isLogin,
  1296. isLogins: isLogins,
  1297. payOrder: payOrder,
  1298. payOrders: payOrders,
  1299. getLocation: getLocation,
  1300. scopeAddress: scopeAddress,
  1301. isAuth: isAuth,
  1302. alertMsg: alertMsg,
  1303. getToken: getToken,
  1304. loadIng: loadIng,
  1305. removeStorage: removeStorage,
  1306. public: publics,
  1307. header: header,
  1308. // gettmplIds: gettmplIds,
  1309. excludeSpecial: excludeSpecial,
  1310. tab: tab,
  1311. msgConfirmText: msgConfirmText,
  1312. silenceLogin: silenceLogin,
  1313. getWXUserProfile: getWXUserProfile,
  1314. updataWxInfos: updataWxInfos,
  1315. saveBehavior: saveBehavior,
  1316. getBehavior: getBehavior,
  1317. saveBehaviorNew:saveBehaviorNew,
  1318. unitConverter: unitConverter,
  1319. initIMChat: initIMChat,
  1320. getIMToken: getIMToken,
  1321. jumpConversation: jumpConversation,
  1322. jumpSingleChatRoom: jumpSingleChatRoom,
  1323. getsysConfig:getsysConfig
  1324. }