common.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import config from './common.js'
  2. // import storage from './storge.js'
  3. import Cache from './cache'
  4. export const babyImg = '/static/images/my/default_baby.png'
  5. export default {
  6. // 平台币转人民币
  7. gstToRmb(amount) {
  8. let coinToRmb = Cache.get('coinToRmb')
  9. let rmb = amount * 1 * coinToRmb
  10. return rmb
  11. },
  12. // 返回上一页
  13. back() {
  14. uni.navigateBack({
  15. delta: 1
  16. })
  17. },
  18. // 提示框
  19. toast(msg, callback = false) {
  20. uni.showToast({
  21. icon: 'none',
  22. title: msg,
  23. duration: 2000
  24. })
  25. if (callback !== false) {
  26. setTimeout(() => {
  27. callback()
  28. }, 1500)
  29. }
  30. },
  31. // 复制文本
  32. copy(text) {
  33. let self = this;
  34. uni.setClipboardData({
  35. data: text,
  36. success: function() {
  37. uni.showToast({
  38. icon: 'none',
  39. title: '已复制到剪切板'
  40. })
  41. }
  42. });
  43. },
  44. // 复制文本或打电话
  45. callOrCopy(phoneNumber) {
  46. uni.showActionSheet({
  47. itemList: ['拨打电话', '复制'],
  48. success: function(res) {
  49. // console.log(res)
  50. if (res.tapIndex === 0) {
  51. let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
  52. if (!phoneReg.test(phoneNumber)) {
  53. uni.showToast({
  54. title: '该号码不是手机号,无法拨打',
  55. icon: 'none'
  56. })
  57. return
  58. } else {
  59. uni.makePhoneCall({
  60. phoneNumber: phoneNumber
  61. });
  62. }
  63. } else {
  64. this.copy(phoneNumber)
  65. }
  66. },
  67. fail: function(res) {
  68. console.log(res.errMsg);
  69. }
  70. });
  71. },
  72. // 暂未开放
  73. unopen() {
  74. uni.showModal({
  75. content: '暂未开放'
  76. })
  77. },
  78. //上传图片
  79. uploadImg() {
  80. let base_url = config.base_api
  81. return new Promise((resolve) => {
  82. uni.chooseImage({
  83. sourceType: ['album'], //从相册选择
  84. success: (res) => {
  85. uni.showLoading({
  86. title: '图片上传中',
  87. mask: true,
  88. success: () => {
  89. const tempFilePaths = res.tempFilePaths;
  90. uni.uploadFile({
  91. url: base_url + '/ajax/upload',
  92. filePath: tempFilePaths[0],
  93. name: 'file',
  94. header: {
  95. // 'XX-Device-Type': this.$storage.get('model'),
  96. // 'token': this.$storage.get('token')
  97. 'token': 'e3ddddf5-9ec8-4189-9099-5b81b76e8a45'
  98. },
  99. success: (uploadFileRes) => {
  100. let res = uploadFileRes.data
  101. res = JSON.parse(res)
  102. if (res.code === 1) {
  103. uni.showModal({
  104. title: '图片上传成功',
  105. showCancel: false,
  106. success: (re) => {
  107. if (re
  108. .confirm) {
  109. // this.sj_pz = tempFilePaths[0]
  110. // this.sj = res.data.path
  111. console.log(
  112. res);
  113. // url = res.data.path
  114. resolve(res
  115. .data
  116. .url
  117. )
  118. }
  119. }
  120. })
  121. } else {
  122. this.$toast('上传失败,请重新上传')
  123. }
  124. },
  125. complete: () => {
  126. uni.hideLoading()
  127. }
  128. })
  129. }
  130. })
  131. }
  132. })
  133. })
  134. },
  135. // 保存图片
  136. savePhoto(val) {
  137. uni.showModal({
  138. title: "保存二维码",
  139. success: (res) => {
  140. if (res.confirm) {
  141. uni.saveImageToPhotosAlbum({
  142. filePath: val,
  143. success: () => {
  144. this.toast("保存成功")
  145. }
  146. });
  147. } else if (res.cancel) {
  148. this.toast("取消保存")
  149. }
  150. }
  151. })
  152. },
  153. // 下载安装包
  154. updateApk(data) {
  155. if (!data.appDownload) return;
  156. uni.showModal({
  157. title: '更新提示',
  158. content: data.up_info,
  159. success: function(res) {
  160. if (res.confirm) {
  161. plus.runtime.openURL(data.appDownload);
  162. } else if (res.cancel) {
  163. console.log('用户点击取消');
  164. }
  165. }
  166. });
  167. },
  168. // 热更新
  169. hotUpdate(data) {
  170. if (!data.wgtDownload) return;
  171. uni.showModal({
  172. title: '更新提示',
  173. content: data.up_info,
  174. success: function(res) {
  175. if (res.confirm) {
  176. uni.showLoading({
  177. title: '正在下载更新',
  178. success() {
  179. uni.downloadFile({
  180. url: data.wgtDownload,
  181. success: (downloadResult) => {
  182. uni.hideLoading();
  183. if (downloadResult.statusCode === 200) {
  184. uni.showLoading({
  185. title: '正在升级',
  186. success() {
  187. plus.runtime.install(
  188. downloadResult
  189. .tempFilePath, {
  190. force: false
  191. },
  192. function() {
  193. uni.hideLoading();
  194. plus.runtime
  195. .restart();
  196. },
  197. function(e) {
  198. console.error(
  199. 'install fail...'
  200. );
  201. uni.showToast({
  202. title: "安装失败",
  203. icon: 'none'
  204. })
  205. });
  206. setTimeout(function() {
  207. uni.hideLoading()();
  208. }, 3000)
  209. }
  210. })
  211. }
  212. }
  213. });
  214. }
  215. })
  216. } else if (res.cancel) {
  217. console.log('用户点击取消');
  218. }
  219. }
  220. })
  221. },
  222. }