cloud-function.uvue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view>
  6. <page-head :title="title"></page-head>
  7. <view class="uni-padding-wrap uni-common-mt">
  8. <view class="uni-btn-v uni-common-mt">
  9. <button type="primary" @click="callFunction">请求云函数</button>
  10. <button type="primary" @click="callFunctionWithGeneric">请求云函数传入泛型</button>
  11. <!-- #ifdef APP-ANDROID || APP-IOS -->
  12. <button type="primary" @click="callEncryptionFunction">请求安全网络加密云函数</button>
  13. <button type="primary" @click="callVerifyFunction">请求安全网络客户端校验云函数</button>
  14. <view><text class="tips">安全网络相关功能需要打包自定义基座方可正常使用</text></view>
  15. <!-- #endif -->
  16. </view>
  17. </view>
  18. </view>
  19. <!-- #ifdef APP -->
  20. </scroll-view>
  21. <!-- #endif -->
  22. </template>
  23. <script>
  24. export default {
  25. data() {
  26. return {
  27. title: '请求云函数',
  28. callFunctionResult: {},
  29. callFunctionResult_Detail_functionName: '',
  30. callFunctionError: {},
  31. genericDemoShowMessage: '',
  32. isUniTest: false
  33. }
  34. },
  35. onLoad() {
  36. },
  37. onUnload() {
  38. if (this.isUniTest) {
  39. uni.hideToast()
  40. }
  41. },
  42. methods: {
  43. notify(content : string, title : string) {
  44. if (!this.isUniTest) {
  45. uni.showModal({
  46. title,
  47. content,
  48. showCancel: false
  49. })
  50. } else {
  51. console.log(title, content)
  52. }
  53. },
  54. async callFunctionWithGeneric(): Promise<void> {
  55. type EchoCfResult = {
  56. showMessage : string
  57. }
  58. uni.showLoading({
  59. title: '加载中...'
  60. })
  61. await uniCloud.callFunction<EchoCfResult>({
  62. name: 'echo-cf',
  63. data: {
  64. num: 1,
  65. str: 'ABC'
  66. }
  67. }).then(res => {
  68. const result = res.result
  69. uni.hideLoading()
  70. this.genericDemoShowMessage = result.showMessage
  71. this.notify(result.showMessage, '提示')
  72. }).catch((err : any | null) => {
  73. const error = err as UniCloudError
  74. this.callFunctionError = {
  75. errCode: error.errCode,
  76. errMsg: error.errMsg
  77. }
  78. uni.hideLoading()
  79. this.notify(error.errMsg, '错误')
  80. })
  81. },
  82. async callFunction(): Promise<void> {
  83. uni.showLoading({
  84. title: '加载中...'
  85. })
  86. await uniCloud.callFunction({
  87. name: 'echo-cf',
  88. data: {
  89. num: 1,
  90. str: 'ABC'
  91. }
  92. }).then(res => {
  93. const result = res.result
  94. this.callFunctionResult = result
  95. const detail = result.get('detail') as UTSJSONObject
  96. this.callFunctionResult_Detail_functionName = detail.get('functionName') as string
  97. console.log('this.callFunctionResult_Detail_functionName: ' + this.callFunctionResult_Detail_functionName)
  98. console.log(JSON.stringify(result))
  99. uni.hideLoading()
  100. this.notify(result['showMessage'] as string, '提示')
  101. }).catch((err : any | null) => {
  102. uni.hideLoading()
  103. if(err instanceof UniCloudError) {
  104. const error = err as UniCloudError
  105. this.callFunctionError = {
  106. errCode: error.errCode,
  107. errMsg: error.errMsg
  108. }
  109. this.notify(error.errMsg, '错误')
  110. } else {
  111. console.error(err)
  112. }
  113. })
  114. },
  115. callEncryptionFunction() {
  116. uni.showLoading({
  117. title: '加载中...'
  118. })
  119. uniCloud.callFunction({
  120. name: 'encryption',
  121. data: {},
  122. secretType: 'both'
  123. }).then(res => {
  124. uni.hideLoading()
  125. this.notify(JSON.stringify(res.result), '提示')
  126. }).catch((err : any | null) => {
  127. uni.hideLoading()
  128. if(err instanceof UniCloudError) {
  129. const error = err as UniCloudError
  130. this.callFunctionError = {
  131. errCode: error.errCode,
  132. errMsg: error.errMsg
  133. }
  134. this.notify(error.errMsg, '错误')
  135. } else {
  136. console.error(err)
  137. }
  138. })
  139. },
  140. callVerifyFunction() {
  141. uni.showLoading({
  142. title: '加载中...'
  143. })
  144. uniCloud.callFunction({
  145. name: 'verify-client',
  146. data: {}
  147. }).then(res => {
  148. uni.hideLoading()
  149. this.notify(JSON.stringify(res.result), '提示')
  150. }).catch((err : any | null) => {
  151. uni.hideLoading()
  152. if(err instanceof UniCloudError) {
  153. const error = err as UniCloudError
  154. this.callFunctionError = {
  155. errCode: error.errCode,
  156. errMsg: error.errMsg
  157. }
  158. this.notify(error.errMsg, '错误')
  159. } else {
  160. console.error(err)
  161. }
  162. })
  163. },
  164. jest_UniCloudError() {
  165. return new Error() instanceof UniCloudError
  166. }
  167. }
  168. }
  169. </script>
  170. <style>
  171. .tips {
  172. color: #999999;
  173. font-size: 12px;
  174. padding: 10px 0px;
  175. }
  176. </style>