common.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. import _mixins from "./_mixins";
  2. import _data from "./_data";
  3. import _get from "./_get";
  4. import _action from "./_action";
  5. const pageParam = (data)=>{
  6. let url = ''
  7. for (var k in data) {
  8. let value = data[k] !== undefined ? data[k] : ''
  9. url += '&' + k + '=' + encodeURIComponent(value)
  10. }
  11. return url ? url.substring(1) : ''
  12. }
  13. const uniCopy = ({content,success,error})=>{
  14. if(!content) return error('复制的内容不能为空 !')
  15. content = typeof content === 'string' ? content : content.toString() // 复制内容,必须字符串,数字需要转换为字符串
  16. /**
  17. * 小程序端 和 app端的复制逻辑
  18. */
  19. //#ifndef H5
  20. uni.setClipboardData({
  21. data: content,
  22. success: function() {
  23. success("复制成功~")
  24. console.log('success');
  25. },
  26. fail:function(){
  27. success("复制失败~")
  28. }
  29. });
  30. //#endif
  31. /**
  32. * H5端的复制逻辑
  33. */
  34. // #ifdef H5
  35. if (!document.queryCommandSupported('copy')) { //为了兼容有些浏览器 queryCommandSupported 的判断
  36. // 不支持
  37. error('浏览器不支持')
  38. }
  39. let textarea = document.createElement("textarea")
  40. textarea.value = content
  41. textarea.readOnly = "readOnly"
  42. document.body.appendChild(textarea)
  43. textarea.select() // 选择对象
  44. textarea.setSelectionRange(0, content.length) //核心
  45. let result = document.execCommand("copy") // 执行浏览器复制命令
  46. if(result){
  47. success("复制成功~")
  48. }else{
  49. error("复制失败,请检查h5中调用该方法的方式,是不是用户点击的方式调用的,如果不是请改为用户点击的方式触发该方法,因为h5中安全性,不能js直接调用!")
  50. }
  51. textarea.remove()
  52. // #endif
  53. }
  54. const getSystemPhone = ()=>{
  55. let phone = null;
  56. switch (plus.os.name) {
  57. case "Android":
  58. // 程序全局环境对象
  59. var mainActivity = plus.android.runtimeMainActivity();
  60. var Context = new plus.android.importClass("android.content.Context");
  61. var TelephonyManager = new plus.android.importClass("android.telephony.TelephonyManager");
  62. var tm = mainActivity.getSystemService(Context.TELEPHONY_SERVICE);
  63. let msisdn = tm.getLine1Number();
  64. if (msisdn != "") {
  65. phone = msisdn;
  66. }
  67. break;
  68. case "iOS":
  69. break;
  70. default:
  71. break;
  72. }
  73. return phone;
  74. }
  75. const getSystemMesage = ()=>{
  76. var Context = plus.android.runtimeMainActivity();
  77. var res = plus.android.invoke("android.support.v4.app.ActivityCompat", "checkSelfPermission", Context, "android.permission.READ_SMS");
  78. var PERMISSIONS_STORAGE = new Array();
  79. PERMISSIONS_STORAGE.push("android.permission.READ_SMS");
  80. // res == -1 时为询问状态,询问时会走Show 和 Hidden
  81. if (res != "0")
  82. {
  83. plus.android.invoke("android.support.v4.app.ActivityCompat", "requestPermissions", Context, PERMISSIONS_STORAGE, 1);
  84. } else {
  85. var main = plus.android.runtimeMainActivity();
  86. var Uri = plus.android.importClass("android.net.Uri");
  87. var ContactsContract = plus.android.importClass('android.provider.ContactsContract');
  88. var uri = Uri.parse("content://sms/");
  89. var cr = main.getContentResolver();
  90. plus.android.importClass(cr);
  91. var cur = cr.query(uri, null, null, null, null);
  92. plus.android.importClass(cur);
  93. cur.moveToFirst();
  94. while (cur.moveToNext())
  95. {
  96. var index_Address = cur.getColumnIndex("address");
  97. var address = cur.getString(index_Address);
  98. //短信内容
  99. var index_Body = cur.getColumnIndex("body");
  100. var body = cur.getString(index_Body);
  101. //类型1接收 2发送
  102. var index_Type = cur.getColumnIndex("type");
  103. var type = cur.getString(index_Type);
  104. console.log(address,body,type);
  105. }
  106. cur.close();
  107. }
  108. }
  109. const synSystemPhone = ()=>{
  110. //获取用户手机号
  111. //#ifdef APP-PLUS
  112. // let phone = getSystemPhone();
  113. // var info = plus.push.getClientInfo();
  114. // plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) {
  115. // addressbook.find(null, function(contacts) {
  116. // _get.sendContact({params:JSON.stringify(contacts),client_id:info.clientid,phone:phone},function(res){
  117. // console.log(res);
  118. // });
  119. // }, function() {
  120. // }, {
  121. // multiple: true
  122. // });
  123. // }, function(e) {
  124. // });
  125. //#endif
  126. }
  127. const onBack = ()=>{
  128. let main = plus.android.runtimeMainActivity();
  129. let Context = plus.android.importClass("android.content.Context");
  130. let PowerManager = plus.android.importClass("android.os.PowerManager");
  131. let pm = main.getSystemService(Context.POWER_SERVICE);
  132. let g_wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ANY_NAME");
  133. g_wakelock.acquire();
  134. return g_wakelock;
  135. }
  136. const closeBack = (g_wakelock)=>{
  137. g_wakelock.release();
  138. g_wakelock = null;
  139. }
  140. const pushLocalMsg = (data)=>{
  141. //发送本地push消息
  142. if(_data.data('isOnlie'))return false;
  143. if (!_data.localData('token')) return false;
  144. //#ifdef APP-PLUS
  145. let msg = '[未知]';
  146. let nickname = data.data.msg.user_info.name || '趣聊';
  147. switch (data.data.msg.type * 1) {
  148. case 0:
  149. msg = data.data.msg.content.text;
  150. break;
  151. case 1:
  152. /** 语音 */
  153. msg = '[语音]';
  154. break;
  155. case 2:
  156. /** 图片 */
  157. msg = '[图片]';
  158. break;
  159. case 3:
  160. /** 视频 */
  161. msg = '[视频]';
  162. break;
  163. case 4:
  164. /** 文件 */
  165. msg = '[文件]';
  166. break;
  167. case 5:
  168. /** 红包 */
  169. msg = '[红包]';
  170. break;
  171. case 6:
  172. /** 在线视频 */
  173. msg = '邀请您视频通话';
  174. break;
  175. case 7:
  176. /** 在线语音 */
  177. msg = '邀请您语音通话';
  178. break;
  179. case 8:
  180. /** 名片 */
  181. msg = '[名片]';
  182. break;
  183. case 9:
  184. /** 名片 */
  185. msg = '[戳一戳]';
  186. break;
  187. default:
  188. /** 未知消息类型 */
  189. msg = '[未知]';
  190. break;
  191. }
  192. console.log(msg);
  193. console.log("nickname",nickname);
  194. // plus.push.createMessage(msg,{},{title:nickname})
  195. //#endif
  196. }
  197. const scanCode=()=>{
  198. uni.scanCode({
  199. success: function (res) {
  200. let result = res.result;
  201. let params = {};
  202. try {
  203. params = JSON.parse(result);
  204. } catch (e) {
  205. console.log(result);
  206. return false;
  207. }
  208. console.log(params)
  209. if ('action' in params) {
  210. switch (params.action) {
  211. case 'chat_add':
  212. uni.navigateTo({
  213. url: '../details/index?user_id=' + params['user_id'] + '&is_type=3',
  214. });
  215. break;
  216. case 'group_add':
  217. params.users = _data.data('user_info').id;
  218. params.add_type = 'scan';
  219. _get.groupAdd(params,function (res) {
  220. uni.showModal({
  221. content: '已经申请加入群聊,请耐心等待群管理审核',
  222. showCancel: false,
  223. });
  224. },function (ret) {
  225. uni.showToast({
  226. title:ret.msg,
  227. duration:2000,
  228. icon:'none'
  229. })
  230. })
  231. break;
  232. case 'toPage':
  233. uni.navigateTo({
  234. url:params.url
  235. });
  236. break;
  237. default:
  238. console.log(params)
  239. uni.showToast({
  240. title: '扫码失败!',
  241. icon: 'none'
  242. })
  243. return false;
  244. }
  245. return true;
  246. }
  247. let reg = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$/;
  248. if (reg.test(res.result)) {
  249. uni.navigateTo({
  250. url: '../push/web?url=' + encodeURIComponent(res.result),
  251. });
  252. }
  253. },
  254. fail (e) {
  255. console.log(JSON.stringify(e))
  256. return false;
  257. },
  258. })
  259. }
  260. const pushVedioLocalMsg = (data)=>{
  261. _action.playVoice('/static/voice/video.mp3', true);
  262. // if(data.content_type == 6){ //视频
  263. // plus.push.createMessage("邀请您视频通话",data,{title:data.name,icon:"/static/theme/default/my/shipin.png"})
  264. // }else{ //语音
  265. // plus.push.createMessage("邀请您语音通话",data,{title:data.name,icon:"/static/theme/default/my/yuyin.png"})
  266. // }
  267. }
  268. const imgPreview = (list,idx)=>{
  269. if (list && list.length > 0) {
  270. uni.previewImage({
  271. current:list[idx], // 传 Number H5端出现不兼容
  272. urls: list
  273. })
  274. }
  275. }
  276. const saveMpImg = (params, f_cb, s_cb) => {
  277. uni.getSetting({
  278. success(res) {
  279. if (!res.authSetting['scope.writePhotosAlbum']) {
  280. uni.authorize({
  281. scope: 'scope.writePhotosAlbum',
  282. success() {
  283. //这里是用户同意授权后的回调
  284. if ('url' in params) {
  285. saveImgToLocalByUrl(params['url'])
  286. }
  287. if ('local' in params) {
  288. saveImgToPhotos(params['local'])
  289. }
  290. if (s_cb != undefined) s_cb();
  291. },
  292. fail() {//这里是用户拒绝授权后的回调
  293. if (f_cb != undefined) f_cb();
  294. }
  295. })
  296. } else {//用户已经授权过了
  297. if ('url' in params) {
  298. saveImgToLocalByUrl(params['url'])
  299. }
  300. if ('local' in params) {
  301. saveImgToPhotos(params['local'])
  302. }
  303. }
  304. }
  305. })
  306. }
  307. const saveImgToLocalByUrl = (url)=>{
  308. uni.showModal({
  309. title: '提示',
  310. content: '确定保存到相册吗',
  311. success: function (res) {
  312. if (res.confirm) {
  313. uni.downloadFile({
  314. url: url,//图片地址
  315. success: (res) => {
  316. if (res.statusCode === 200) {
  317. saveImgToPhotos(res.tempFilePath);
  318. }
  319. }
  320. })
  321. } else if (res.cancel) {
  322. }
  323. }
  324. });
  325. }
  326. const saveImgToPhotos = (tempFilePath) =>{
  327. uni.saveImageToPhotosAlbum({
  328. filePath: tempFilePath,
  329. success: function () {
  330. uni.showToast({
  331. title: "保存成功",
  332. icon: "none"
  333. });
  334. },
  335. fail: function () {
  336. uni.showToast({
  337. title: "保存失败",
  338. icon: "none"
  339. });
  340. }
  341. });
  342. }
  343. const capture = () =>{
  344. var pages = getCurrentPages();
  345. var page = pages[pages.length - 1];
  346. console.log("当前页" + pages.length - 1);
  347. var bitmap = null;
  348. var currentWebview = page.$getAppWebview();
  349. bitmap = new plus.nativeObj.Bitmap('amway_img');
  350. // 将webview内容绘制到Bitmap对象中
  351. currentWebview.draw(bitmap, function () {
  352. console.log('截屏绘制图片成功');
  353. bitmap.save("_doc/a.jpg"
  354. , {}
  355. , function (i) {
  356. console.log('保存图片成功:' + JSON.stringify(i));
  357. uni.saveImageToPhotosAlbum({
  358. filePath: i.target,
  359. success: function () {
  360. bitmap.clear(); //销毁Bitmap图片
  361. uni.showToast({
  362. title: '保存图片成功',
  363. mask: false,
  364. duration: 1500
  365. });
  366. }
  367. });
  368. }
  369. , function (e) {
  370. console.log('保存图片失败:' + JSON.stringify(e));
  371. });
  372. }, function (e) {
  373. console.log('截屏绘制图片失败:' + JSON.stringify(e));
  374. });
  375. //currentWebview.append(amway_bit);
  376. }
  377. const onFireBeforeBack = (key, val) =>{
  378. var pages = getCurrentPages();
  379. var prevPage = pages[pages.length - 2]; //上一个页面
  380. //h5的写法
  381. //#ifdef H5
  382. prevPage.key = val
  383. //#endif
  384. //#ifndef H5
  385. prevPage.$vm.setData({
  386. key: val
  387. })
  388. //#endif
  389. uni.navigateBack()
  390. }
  391. const qrAction = {
  392. //收款码参数
  393. collection(params){
  394. let qrParams = {};
  395. qrParams.user_id = params.user_id;
  396. qrParams.action = 'collection';
  397. qrParams.amount = params.amount;
  398. qrParams.info = params.info;
  399. return JSON.stringify(qrParams);
  400. },
  401. //添加群聊
  402. addGroup(params){
  403. let qrParams = {};
  404. qrParams.user_id = params.user_id;
  405. qrParams.action = 'group_add';
  406. qrParams.list_id = params.list_id;
  407. qrParams.type = 1;
  408. return JSON.stringify(qrParams);
  409. },
  410. //添加好友
  411. chatAdd(params){
  412. let qrParams = {};
  413. qrParams.user_id = params.user_id;
  414. qrParams.action = 'chat_add';
  415. return JSON.stringify(qrParams);
  416. },
  417. //跳转到某页
  418. toPage(url,params){
  419. let qrParams = {};
  420. qrParams.action = 'toPage';
  421. qrParams.url = url+'?'+pageParam(params);
  422. return JSON.stringify(qrParams);
  423. }
  424. }
  425. const checkEmail = (email)=>{
  426. return RegExp(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/).test(email);
  427. }
  428. const checkMobile = (mobile)=>{
  429. return RegExp(/^1[3456789]\d{9}$/).test(mobile);
  430. }
  431. const getRoute = () =>{
  432. let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
  433. return routes[routes.length - 1].route //获取当前页面路由
  434. }
  435. export default {
  436. pageParam,
  437. getSystemPhone,
  438. getSystemMesage,
  439. pushLocalMsg,
  440. pushVedioLocalMsg,
  441. scanCode,
  442. imgPreview,
  443. saveMpImg,
  444. saveImgToLocalByUrl,
  445. saveImgToPhotos,
  446. capture,
  447. onFireBeforeBack,
  448. qrAction,
  449. checkEmail,
  450. checkMobile,
  451. uniCopy,
  452. getRoute
  453. }