pyip.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // pages/pyip/pyip.js
  2. const COS = require('../../utils/cos.js');
  3. const app = require('../../utils/util.js');
  4. const tools = require('../../utils/tools.js');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. carWin_img_hidden: true, //展示照片的view是否隐藏
  11. carWin_img: '', //存放照片路径的
  12. carWin_img_type: 2, //是图片还是视频
  13. cosPath: "" //腾讯云上传的路径
  14. },
  15. openCamera() {
  16. wx.chooseMedia({
  17. count: 1,
  18. mediaType: ['image', 'video'],
  19. sourceType: ['album', 'camera'],
  20. maxDuration: 30,
  21. camera: 'back',
  22. success: (res) => {
  23. console.log(res, "9999999999999999")
  24. let path = res.tempFiles[0].tempFilePath
  25. let uploadType = path.substring(path.length - 3)
  26. if (uploadType == "jpg" || uploadType == "png") {
  27. this.setData({
  28. carWin_img: path,
  29. carWin_img_hidden: false,
  30. carWin_img_type: 0
  31. })
  32. } else if (uploadType == "mp4") {
  33. this.setData({
  34. carWin_img: path,
  35. carWin_img_hidden: false,
  36. carWin_img_type: 1
  37. })
  38. }
  39. }
  40. })
  41. },
  42. submitImgToTxy() {
  43. if (this.data.carWin_img == "" || this.data.carWin_img == null) {
  44. wx.showModal({
  45. title: '请点击上方选择照片或者视频哦',
  46. showCancel: false
  47. });
  48. return false
  49. }
  50. wx.showLoading({
  51. mask: true,
  52. title: '上传中',
  53. })
  54. var Bucket = 'bucketnj1-1254259530';
  55. var Region = 'ap-nanjing';
  56. var ForcePathStyle = false;
  57. var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/';
  58. if (ForcePathStyle) {
  59. prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/';
  60. }
  61. var stsCache;
  62. var getCredentials = function (callback) {
  63. if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) {
  64. callback(data.credentials);
  65. return;
  66. }
  67. wx.request({
  68. method: 'POST',
  69. url: app.globalData.publicUrl + '/wx/course/getSts',
  70. // dataType: 'json',
  71. success: function (result) {
  72. var credentials = result.data.credential.credentials;
  73. if (credentials) {
  74. stsCache = result.data.credential
  75. } else {
  76. wx.showModal({
  77. title: '临时密钥获取失败',
  78. content: JSON.stringify(data),
  79. showCancel: false
  80. });
  81. }
  82. callback(stsCache && stsCache.credentials);
  83. },
  84. error: function (err) {
  85. wx.showModal({
  86. title: '临时密钥获取失败',
  87. content: JSON.stringify(err),
  88. showCancel: false
  89. });
  90. }
  91. });
  92. };
  93. // 计算签名
  94. var getAuthorization = function (options, callback) {
  95. getCredentials(function (credentials) {
  96. callback({
  97. XCosSecurityToken: credentials.sessionToken,
  98. Authorization: COS({
  99. SecretId: credentials.tmpSecretId,
  100. SecretKey: credentials.tmpSecretKey,
  101. Method: options.Method,
  102. Pathname: options.Pathname,
  103. })
  104. });
  105. });
  106. };
  107. // 上传文件
  108. var Key = this.data.carWin_img.substr(this.data.carWin_img.lastIndexOf('/') + 1); // 这里指定上传的文件名
  109. var signPathname = '/';
  110. if (ForcePathStyle) {
  111. signPathname = '/' + Bucket + '/';
  112. }
  113. getAuthorization({
  114. Method: 'POST',
  115. Pathname: signPathname
  116. }, (AuthData) => {
  117. var requestTask = wx.uploadFile({
  118. url: prefix,
  119. name: 'file',
  120. filePath: this.data.carWin_img,
  121. formData: {
  122. 'key': "paiyipai/" + Key,
  123. 'success_action_status': 200,
  124. 'Signature': AuthData.Authorization,
  125. 'x-cos-security-token': AuthData.XCosSecurityToken,
  126. 'Content-Type': '',
  127. },
  128. success: (res) => {
  129. if (/^2\d\d$/.test('' + res.statusCode)) {
  130. wx.request({
  131. method: 'PUT',
  132. url: app.globalData.publicUrl + '/wx/shoot',
  133. data: {
  134. shootName: this.data.carWin_img,
  135. shootCosName: Key,
  136. sessionKey: this.data.sessionKey,
  137. shootType: this.data.carWin_img_type
  138. },
  139. success: (res) => {
  140. if (res.data.code == 0) {
  141. console.log(res, "我把照片路径传到服务器啦")
  142. wx.showModal({
  143. title: '上传成功',
  144. showCancel: false
  145. });
  146. this.setData({
  147. // cosPath: res.header.Location,
  148. carWin_img: "",
  149. carWin_img_type: 2,
  150. carWin_img_hidden: true
  151. })
  152. this.getArr(this.data.sessionKey)
  153. } else {
  154. wx.showModal({
  155. title: "上传失败啦",
  156. showCancel: false
  157. });
  158. }
  159. }
  160. });
  161. } else {
  162. wx.showModal({
  163. title: '上传失败',
  164. showCancel: false
  165. });
  166. }
  167. },
  168. fail: function () {
  169. wx.hideLoading();
  170. wx.showModal({
  171. title: '上传失败',
  172. showCancel: false
  173. });
  174. }
  175. });
  176. requestTask.onProgressUpdate(function (res) {
  177. console.log('进度:', res);
  178. if (res.progress == 100) {
  179. wx.hideLoading();
  180. }
  181. });
  182. });
  183. },
  184. getArr(sessionKey) {
  185. wx.request({
  186. url: app.globalData.publicUrl + '/wx/shoot',
  187. method: "get",
  188. data: {
  189. sessionKey
  190. },
  191. success: (res) => {
  192. console.log(res.data.imgList, "我是查到的照片视频列表")
  193. console.log(res.data.videoList, "我是查到的照片视频列表")
  194. this.setData({
  195. imgList: res.data.imgList,
  196. videoList: res.data.videoList
  197. })
  198. }
  199. })
  200. },
  201. /**
  202. * 生命周期函数--监听页面加载
  203. */
  204. async onLoad() {
  205. const sessionKey = await tools.checkSessionAndLogin();
  206. this.setData({
  207. sessionKey
  208. })
  209. this.getArr(sessionKey)
  210. // this.getArr(sessionKey);
  211. },
  212. /**
  213. * 生命周期函数--监听页面初次渲染完成
  214. */
  215. onReady: function () {
  216. this.setData({
  217. carWin_img_hidden: true,
  218. carWin_img: ''
  219. });
  220. },
  221. /**
  222. * 生命周期函数--监听页面显示
  223. */
  224. onShow: function () {
  225. },
  226. /**
  227. * 生命周期函数--监听页面隐藏
  228. */
  229. onHide: function () {
  230. },
  231. /**
  232. * 生命周期函数--监听页面卸载
  233. */
  234. onUnload: function () {
  235. },
  236. /**
  237. * 页面相关事件处理函数--监听用户下拉动作
  238. */
  239. onPullDownRefresh: function () {
  240. },
  241. /**
  242. * 页面上拉触底事件的处理函数
  243. */
  244. onReachBottom: function () {
  245. },
  246. /**
  247. * 用户点击右上角分享
  248. */
  249. onShareAppMessage: function () {
  250. }
  251. })