pyip.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.hideLoading();
  77. wx.showModal({
  78. title: '临时密钥获取失败',
  79. content: JSON.stringify(data),
  80. showCancel: false
  81. });
  82. }
  83. callback(stsCache && stsCache.credentials);
  84. },
  85. error: function (err) {
  86. wx.hideLoading();
  87. wx.showModal({
  88. title: '临时密钥获取失败',
  89. content: JSON.stringify(err),
  90. showCancel: false
  91. });
  92. }
  93. });
  94. };
  95. // 计算签名
  96. var getAuthorization = function (options, callback) {
  97. getCredentials(function (credentials) {
  98. callback({
  99. XCosSecurityToken: credentials.sessionToken,
  100. Authorization: COS({
  101. SecretId: credentials.tmpSecretId,
  102. SecretKey: credentials.tmpSecretKey,
  103. Method: options.Method,
  104. Pathname: options.Pathname,
  105. })
  106. });
  107. });
  108. };
  109. // 上传文件
  110. var Key = this.data.carWin_img.substr(this.data.carWin_img.lastIndexOf('/') + 1); // 这里指定上传的文件名
  111. var signPathname = '/';
  112. if (ForcePathStyle) {
  113. signPathname = '/' + Bucket + '/';
  114. }
  115. getAuthorization({
  116. Method: 'POST',
  117. Pathname: signPathname
  118. }, (AuthData) => {
  119. var requestTask = wx.uploadFile({
  120. url: prefix,
  121. name: 'file',
  122. filePath: this.data.carWin_img,
  123. formData: {
  124. 'key': "paiyipai/" + Key,
  125. 'success_action_status': 200,
  126. 'Signature': AuthData.Authorization,
  127. 'x-cos-security-token': AuthData.XCosSecurityToken,
  128. 'Content-Type': '',
  129. },
  130. success: (res) => {
  131. if (/^2\d\d$/.test('' + res.statusCode)) {
  132. wx.request({
  133. method: 'PUT',
  134. url: app.globalData.publicUrl + '/wx/shoot',
  135. data: {
  136. shootName: this.data.carWin_img,
  137. shootCosName: Key,
  138. sessionKey: this.data.sessionKey,
  139. shootType: this.data.carWin_img_type
  140. },
  141. success: (res) => {
  142. if (res.data.code == 0) {
  143. wx.hideLoading();
  144. wx.showModal({
  145. title: '上传成功',
  146. showCancel: false
  147. });
  148. this.setData({
  149. // cosPath: res.header.Location,
  150. carWin_img: "",
  151. carWin_img_type: 2,
  152. carWin_img_hidden: true
  153. })
  154. this.getArr(this.data.sessionKey)
  155. } else {
  156. wx.hideLoading();
  157. wx.showModal({
  158. title: res.data.msg,
  159. showCancel: false
  160. });
  161. }
  162. }
  163. });
  164. } else {
  165. wx.hideLoading();
  166. wx.showModal({
  167. title: '上传失败',
  168. showCancel: false
  169. });
  170. }
  171. },
  172. fail: function () {
  173. wx.hideLoading();
  174. wx.showModal({
  175. title: '上传失败',
  176. showCancel: false
  177. });
  178. }
  179. });
  180. requestTask.onProgressUpdate(function (res) {
  181. console.log('进度:', res);
  182. });
  183. });
  184. },
  185. getArr(sessionKey) {
  186. wx.request({
  187. url: app.globalData.publicUrl + '/wx/shoot',
  188. method: "get",
  189. data: {
  190. sessionKey
  191. },
  192. success: (res) => {
  193. console.log(res.data.imgList, "我是查到的照片视频列表")
  194. console.log(res.data.videoList, "我是查到的照片视频列表")
  195. this.setData({
  196. imgList: res.data.imgList,
  197. videoList: res.data.videoList
  198. })
  199. if (res.data.imgList && res.data.imgList.length != 0) {
  200. this.setData({
  201. isPhoto: true
  202. })
  203. }
  204. if (res.data.videoList && res.data.videoList.length != 0) {
  205. this.setData({
  206. isVideo: true
  207. })
  208. }
  209. }
  210. })
  211. },
  212. /**
  213. * 生命周期函数--监听页面加载
  214. */
  215. async onLoad() {
  216. const sessionKey = await tools.checkSessionAndLogin();
  217. this.setData({
  218. sessionKey
  219. })
  220. this.getArr(sessionKey)
  221. // this.getArr(sessionKey);
  222. },
  223. /**
  224. * 生命周期函数--监听页面初次渲染完成
  225. */
  226. onReady: function () {
  227. this.setData({
  228. carWin_img_hidden: true,
  229. carWin_img: ''
  230. });
  231. },
  232. /**
  233. * 生命周期函数--监听页面显示
  234. */
  235. onShow: function () {
  236. },
  237. /**
  238. * 生命周期函数--监听页面隐藏
  239. */
  240. onHide: function () {
  241. },
  242. /**
  243. * 生命周期函数--监听页面卸载
  244. */
  245. onUnload: function () {
  246. },
  247. /**
  248. * 页面相关事件处理函数--监听用户下拉动作
  249. */
  250. onPullDownRefresh: function () {
  251. },
  252. /**
  253. * 页面上拉触底事件的处理函数
  254. */
  255. onReachBottom: function () {
  256. },
  257. /**
  258. * 用户点击右上角分享
  259. */
  260. onShareAppMessage: function () {
  261. }
  262. })