index.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import React from "react";
  2. import { View, Text, Image, Button, Canvas } from "@tarojs/components";
  3. import Taro from "@tarojs/taro";
  4. import "./index.scss";
  5. import { InputBox } from "../../component/input/index";
  6. import right from "../../images/right.png";
  7. import application from "../../images/application.png";
  8. import certificate from "../../images/certificate.png";
  9. import expert from "../../images/expert.png";
  10. import user from "../../images/user.png";
  11. import base from "../../config/base";
  12. class Us extends React.Component<any, any> {
  13. static defaultProps = {};
  14. #systemInfo = Taro.getSystemInfoSync();
  15. constructor(props: any) {
  16. super(props);
  17. this.state = {
  18. height: 0,
  19. heightKf: 0,
  20. Cwidth: 0,
  21. Cheight: 0,
  22. userUrl: Taro.getStorageSync("userUrl") || "",
  23. userPhone: Taro.getStorageSync("userPhone") || "",
  24. };
  25. }
  26. getImageSize = (e: any) => {
  27. this.setState({
  28. height: (this.#systemInfo.screenWidth / e.detail.width) * e.detail.height,
  29. });
  30. };
  31. getImageKf = (e: any) => {
  32. this.setState({
  33. heightKf:
  34. ((this.#systemInfo.screenWidth * 0.92) / e.detail.width) *
  35. e.detail.height,
  36. });
  37. };
  38. onChooseAvatar = (e: any) => {
  39. Taro.setStorageSync("userUrl", e.detail.avatarUrl);
  40. this.setState({
  41. userUrl: e.detail.avatarUrl,
  42. });
  43. };
  44. onGetPhoneNumber = (e: any) => {
  45. Taro.request({
  46. url: base.baseAPI + "/wx/getuserphonenumber?code=" + e.detail.code,
  47. method: "GET",
  48. }).then((res: any) => {
  49. const {
  50. data: {
  51. code,
  52. data: { phoneInfo },
  53. },
  54. statusCode,
  55. } = res;
  56. if (statusCode !== 200 || code !== 0)
  57. return Taro.showToast({
  58. title: "获取手机号失败",
  59. icon: "none",
  60. });
  61. const { phoneNumber } = phoneInfo;
  62. this.setState({
  63. userPhone: phoneNumber || "",
  64. });
  65. Taro.setStorageSync("userPhone", phoneNumber || "");
  66. });
  67. };
  68. createImge = () => {
  69. Taro.showLoading({
  70. title: "下载中。。。"
  71. });
  72. const ctx = Taro.createCanvasContext("myCanvas");
  73. // 获取远程图片
  74. Taro.getImageInfo({
  75. src: "https://cxzx.smcic.net/topic/tool/img/%E5%B0%91%E5%84%BF%E4%B9%A6%E7%94%BB%E5%A4%A7%E8%B5%9B/6.jpg",
  76. }).then((res: any) => {
  77. const { width, height, path } = res;
  78. const h = Number(((this.#systemInfo.windowWidth / width) * height).toFixed(2));
  79. const rWidth = this.#systemInfo.windowWidth * this.#systemInfo.pixelRatio;
  80. const rHeight = h * this.#systemInfo.pixelRatio;
  81. this.setState({
  82. Cwidth: rWidth,
  83. Cheight: rHeight
  84. });
  85. Taro.nextTick(() => {
  86. ctx.drawImage(
  87. path,
  88. 0,
  89. 0,
  90. rWidth,
  91. rHeight
  92. );
  93. ctx.setFillStyle("#91725D");
  94. ctx.setFontSize(rWidth/20);
  95. ctx.fillText("李勇利", rWidth*0.33, rHeight*0.4);
  96. ctx.draw(false, () => {
  97. Taro.canvasToTempFilePath({
  98. canvasId: "myCanvas",
  99. quality: 0.8,
  100. success: (res: any) => {
  101. Taro.hideLoading();
  102. Taro.showToast({title: "下载成功"})
  103. Taro.previewImage({
  104. urls: [res.tempFilePath],
  105. });
  106. },
  107. });
  108. });
  109. });
  110. });
  111. };
  112. render() {
  113. // const { } = this.props;
  114. return (
  115. <View className="Us">
  116. <Canvas
  117. id="myCanvas"
  118. canvas-id="myCanvas"
  119. style={`width: ${this.state.Cwidth}px;height: ${this.state.Cheight}px;`}
  120. ></Canvas>
  121. <Button
  122. className="user"
  123. open-type="chooseAvatar"
  124. onChooseAvatar={this.onChooseAvatar}
  125. >
  126. <Image src={this.state.userUrl || user} className="userImg"></Image>
  127. </Button>
  128. <Image
  129. style={`width: ${this.#systemInfo.screenWidth}px;height: ${this.state.height
  130. }px;`}
  131. src="https://cxzx.smcic.net/topic/tool/img/%E5%B0%91%E5%84%BF%E4%B9%A6%E7%94%BB%E5%A4%A7%E8%B5%9B/us-top.png"
  132. onLoad={this.getImageSize}
  133. />
  134. <InputBox>
  135. {!this.state.userPhone && (
  136. <Button
  137. className="login"
  138. openType="getPhoneNumber"
  139. onGetPhoneNumber={this.onGetPhoneNumber}
  140. >
  141. 登录
  142. </Button>
  143. )}
  144. {this.state.userPhone && (
  145. <View className="us_cell">
  146. <View className="left">
  147. <Image src={application} className="img"></Image>
  148. <Text className="text">报名信息</Text>
  149. </View>
  150. <View className="right">
  151. <Image src={right} className="img"></Image>
  152. </View>
  153. </View>
  154. )}
  155. {this.state.userPhone && (
  156. <View className="us_cell" onClick={this.createImge}>
  157. <View className="left">
  158. <Image src={certificate} className="img"></Image>
  159. <Text className="text">证书下载</Text>
  160. </View>
  161. <View className="right">
  162. <Image src={right} className="img"></Image>
  163. </View>
  164. </View>
  165. )}
  166. {this.state.userPhone && (
  167. <View className="us_cell">
  168. <View className="left">
  169. <Image src={expert} className="img"></Image>
  170. <Text className="text">专家评审</Text>
  171. </View>
  172. <View className="right">
  173. <Image src={right} className="img"></Image>
  174. </View>
  175. </View>
  176. )}
  177. </InputBox>
  178. <Image
  179. style={`width: ${this.#systemInfo.screenWidth * 0.92}px;height: ${this.state.heightKf
  180. }px;display: block;margin: 0 auto`}
  181. showMenuByLongpress={true}
  182. src="https://cxzx.smcic.net/topic/tool/img/%E5%B0%91%E5%84%BF%E4%B9%A6%E7%94%BB%E5%A4%A7%E8%B5%9B/us-kf.png"
  183. onLoad={this.getImageKf}
  184. />
  185. </View>
  186. );
  187. }
  188. }
  189. export { Us };